Sure. A function's type consists of the types of its argument and its return type. Here we specify that the callback parameter's type must be "function that accepts a number and returns type any":
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback); // not OK
foo.save(numCallback); // OK
If you want, you can define a type alias to encapsulate this:
type NumberCallback = (n: number) => any;
class Foo {
// Equivalent
save(callback: NumberCallback) : void {
callback(42);
}
}
Answer from Ryan Cavanaugh on Stack OverflowSure. A function's type consists of the types of its argument and its return type. Here we specify that the callback parameter's type must be "function that accepts a number and returns type any":
class Foo {
save(callback: (n: number) => any) : void {
callback(42);
}
}
var foo = new Foo();
var strCallback = (result: string) : void => {
alert(result);
}
var numCallback = (result: number) : void => {
alert(result.toString());
}
foo.save(strCallback); // not OK
foo.save(numCallback); // OK
If you want, you can define a type alias to encapsulate this:
type NumberCallback = (n: number) => any;
class Foo {
// Equivalent
save(callback: NumberCallback) : void {
callback(42);
}
}
Here are TypeScript equivalents of some common .NET delegates:
interface Action<T>
{
(item: T): void;
}
interface Func<T,TResult>
{
(item: T): TResult;
}
typescript - Is it a bad practice to pass instance as function parameter? - Software Engineering Stack Exchange
How to pass a value of type Parameters<T> to the function of type T
Is it possible to pass strongly-typed functions as parameters in TypeScript? - TestMu AI Community
How to pass function and arguments of that function as parameters in typescript - Stack Overflow
Videos
So I'd like something like this:
myFunc(4, "foo", true) // some function that takes a particular set of args const wrappedFunction = makeWrappedFunction(myFunc) wrappedFunction(6, "bar", false)
I'd like the type of arg types and return type of wrappedFunction to match the original. I thought I could do:
type Func = (...args: any) => any
async function wrap<T extends Func> (
fn: T, args?: Parameters<T>
): Promise<ReturnType<T>> {
// do stuff
if (!args)
return await fn()
else
return await fn(...args) // <--- This fails to compile!
}
The error is that args is not iterable. Now I see that Parameters<T> returns a tuple. But how do I "repass" those args to fn?
Thanks geniuses!!
I'm writing a debounce function that takes a function and returns a debounced version. I'd like to maintain the type safety of the original function in the debounced version so I need to infer the types of the parameters and return value of the original function. I'm struggling with the latter.
This is how I'm currently defining the function:
function debounce<T extends (...args: any[]) => unknown>( func: T )
I'm able to use Parameters<T> to infer the original function's parameter types and enforce those on the returned function.
return (...args: Parameters<T>) => {
<debounce stuff>
return func(...args)
}How do I get the return type of the original function and enforce that?