BrunoLM
BrunoLM

Reputation: 100322

How to define a type that is a function with any signature but a specific return type?

I want to create a type that needs to be a function that when called will always return an object that has the property type: string, but I don't care about the parameters passed to this function.

So for example:

foo(1, 'bar'); // returns { type: '', etc: 1 }
baz('bar', new Date()); // returns { type: '', xyz: 2 }
bar(); // returns { type: '', etc: 3, so: 10 }

All of these should be valid for this type, because I don't care about the parameters I'm using to call the function, all I care is that it returns the property type: string when called.

How can I setup a type like this?

Upvotes: 2

Views: 55

Answers (1)

dbandstra
dbandstra

Reputation: 1314

Try this:

type T = (...args: Array<any>) => {type: string};

Upvotes: 2

Related Questions