Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type '() => void' is not assignable to type '() => {}'

I understand the error message:

Type '() => void' is not assignable to type '() => {}'

Well sort of, it is telling me there is a type casting issue. However I can't work out why the compiler thinks the types are not the same.

The back ground to the code is that I have a typescript class that is given a function and then stores it as a member. I want to be able to initialise the member with an empty 'noop' function so that it don't have to null check it before use.

I have managed to reduce problem down to the following example test code:

export class Test {
    private _noop: () => {};

    constructor(
    ) {
        this._noop = () => { };     //I guess the compiler thinks this is returning in a new empty object using the json syntax
        this._noop = this.noop;     //I would have thought this shoud definitely work
        this._noop = () => undefined;   //This does works
    }

    public noop(): void {
        //Nothing to see here...
    }
}

The three statements in the constructor are all intended to do the same job: initialise the member with a no operation function. However only the last statement works:

this._noop = () => undefined; 

The other two statements produce the compile error.

Does any one know why the compiler can't seem to match the types?

like image 297
Tom Maher Avatar asked Jul 28 '17 08:07

Tom Maher


People also ask

Is not assignable to type => void?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

Is not assignable to type '() => string?

The "Type 'string | undefined' is not assignable to type string" error occurs when a possibly undefined value is assigned to something that expects a string . To solve the error, use the non-null assertion operator or a type guard to verify the value is a string before the assignment.

What does () => void mean TypeScript?

Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.

Is not assignable to parameter of type?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

What does type 'void is not assignable to type 'number' mean?

When you don't return a value from a function, you implicitly return undefined and the function's return type is inferred to be void. The error message "Type 'void' is not assignable to type 'number'" means that we have a value that expects an assignment of type number and we're trying to assign a value of type void to it.

Is the 'this' of a void method assignable to an observable string?

"the 'this' context of type 'void' is not assignable to method's 'this' of type 'observable<string>'." ts2684: the 'this' context of type 'void' is not assignable to method's 'this' of type 'observable '.

Why is the ‘this’ context of ‘void’ not assignable?

The ‘this’ context of type ‘void’ is not assignable to method’s ‘this’ of type ‘Observable< {}>‘. If you are experiencing this error – usually something with your RxJS imports is wrong. So you should go through them manually or simply delete all imports from RxJS in the corresponding file and let the IDE generate them again.

Which types are not assignable in typescript?

All Languages >> TypeScript >> Type 'void' is not assignable to type ' ' 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable< {}>'. Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string' Type 'Timeout' is not assignable to type 'number'.


2 Answers

In your definition private _noop: () => {}; _noop is typed as a function returning an object.

When you assign it as this._noop = () => { }; the function you are trying to assign to _noop is of type () => void.

If you wanted _noop to be function returning nothing then type it as:

private _noop: () => void;
like image 189
Saravana Avatar answered Oct 24 '22 02:10

Saravana


The below definition means, _noop is a function must return an object (including undefined and null).

private _noop: () => {};

it's equal to:

private _noop: () => Object;

you can make all three statements work with:

private _noop: () => any;

or the first statement will work with both of these:

this._noop = () => ({});
this._noop = () => { return {} };
like image 45
Val Avatar answered Oct 24 '22 03:10

Val