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


i understand error message:

type '() => void' not assignable type '() => {}'

well sort of, telling me there type casting issue. can't work out why compiler thinks types not same.

the ground code have typescript class given function , stores member. want able initialise member empty 'noop' function don't have null check before use.

i have managed reduce problem down following example test code:

export class test {     private _noop: () => {};      constructor(     ) {         this._noop = () => { };     //i guess compiler thinks returning in new empty object using json syntax         this._noop = this.noop;     //i have thought shoud work         this._noop = () => undefined;   //this works     }      public noop(): void {         //nothing see here...     } } 

the 3 statements in constructor intended same job: initialise member no operation function. last statement works:

this._noop = () => undefined;  

the other 2 statements produce compile error.

does 1 know why compiler can't seem match types?

in definition private _noop: () => {}; _noop typed function returning object.

when assign this._noop = () => { }; function trying assign _noop of type () => void.

if wanted _noop function returning nothing type as:

private _noop: () => void; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -