Created
August 8, 2018 11:41
-
-
Save xcaptain/2ccfc878dbc0bed4813924614a057494 to your computer and use it in GitHub Desktop.
Argument of type 'Function' is not assignable to parameter of type '(...args: any[]) => void'. Type 'Function' provides no match for the signature '(...args: any[]): void'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function on(event: string, listener: Function) { | |
console.log('on event: ', event, 'typeof listener:', typeof (listener)); | |
listener(); | |
} | |
function on1(event: string, listener: (...args: any[]) => void) { | |
console.log('on event: ', event, 'typeof listener:', typeof (listener)); | |
listener(); | |
} | |
// function createCallback(a: number): Function { | |
// let f = (a: number) => { | |
// console.log('return a:', a); | |
// return (a: number) => { return a + 1 }; | |
// }; | |
// return f(a); | |
// } | |
function createCallback(a: number): (a: number) => number { | |
let f = (a: number) => { | |
console.log('return a:', a); | |
return (a: number) => { return a + 1 }; | |
}; | |
return f(a); | |
} | |
function createHellCallback(a: number) : (a: number) => ((a: number) => number) { | |
let f = (a: number) => { | |
console.log('return a:', a); | |
return (a: number) => { | |
return (a: number) => { | |
return a+1; | |
} | |
}; | |
}; | |
return f(a); | |
} | |
function createHellCallback1(a: number) : (a: number) => any { | |
let f = (a: number) => { | |
console.log('return a:', a); | |
return (a: number) => { | |
return (a: number) => { | |
return a+1; | |
} | |
}; | |
}; | |
return f(a); | |
} | |
// let f = createCallback(1); | |
let f = createHellCallback1(1); | |
console.log('type of f', typeof (f)); | |
// on("start", f); | |
on1("start", f); |
@radiumrasheed Hello, I think the problem you faced is not the same problem I faced. This gist is related to https://stackoverflow.com/questions/51744927/argument-of-type-function-is-not-assignable-to-parameter-of-type-args-an
For your problem, I think you mean
function on1(event: string, listener: (...args: any[]) => void) {
console.log('on event: ', event, 'typeof listener:', typeof (listener));
listener(1, 2);
}
function createCallback(a: number): (a: number, b: number) => number {
let f = (a: number) => {
return (a: number, b: number) => {
console.log('run callback', a, b)
return a + b + 1;
};
};
return f(a);
}
If createCallback
creates a closure, and it takes 2 numbers, then you can call it with listener(1, 2)
. If you want to create a closure that can take listener(new_argument, ...args);
I think you would better define the arguments type first.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi @xcaptain, I have a related to your code not the problem.
I intend to call listeners with an additional parameter, like this
can you help with this, I can't access
listener.arguments