'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?
Solution 1:[1]
You can only change a function expression and not a function declaration. If you store a function expression in a variable and return it from an exported function then you can change it.
const noop = () => {}
const firstMouseUp = () => {
}
let func = firstMouseUp;
export function getFunc () {
return func;
}
export function removeFirstMouseUp() {
func = noop;
}
Play here
Solution 2:[2]
It would seem you are just looking for
export function noop() {}
export function removeFirstMouseUp() {
return(noop);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anatoly |
| Solution 2 | Chai |
