Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is it possible to assign a function to another function in typescript

I want to set current function to null loop in typescript, this is my code looks like:

export function noop() {}

export function removeFirstMouseUp() {
  removeFirstMouseUp = noop;
}

but the compiler told me this error:

 TS2630: Cannot assign to 'removeFirstMouseUp' because it is a function.

assign a function to another function, it seems no problem. why the typescript compiler shows this error? what did I do to fix this problem?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of defining removeFirstMouseUp as a function declaration, rewrite it as a function definition:

export function noop() {}

export let removeFirstMouseUp = function() {
    removeFirstMouseUp = noop;
} 

From ESLint’s no-func-assign page:

JavaScript functions can be written as a FunctionDeclaration function foo() { … } or as a FunctionExpression var foo = function() { … };. While a JavaScript interpreter might tolerate it, overwriting/reassigning a function written as a FunctionDeclaration is often indicative of a mistake or issue.

ESLint and TypeScript are different, however, in this case the error messages are referring to the same thing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading