type Actions =
| ['add', number, number] // should return number
| ['log', string]; // should return void
type Call = (...args: Actions) => Promise<?>;
const call: Call = (...args: Actions): Promise<?> => {
// returns some Promise
}
call('add', 1, 1).then(value => {
// value is then inferred as number
})
call('log', 'Hello World!').then(value => {
// value is then inferred as void
})
How do you base the return value of the Promise from whatever arguments was passed to the function?
Two approaches for you:
Call
type as an overloaded function typeCall
typeThe type you'd want for Call
is an overloaded function type. You can define it like this:
type Call = {
(...args: ['add', number, number]): Promise<number>;
(...args: ['log', string]): Promise<void>;
};
Since you need to associate a return type with the parameter list, the Actions
type doesn't really help.
A function typed with that type will do the inference you've asked for:
function doSomething(fn: Call) {
fn('add', 1, 2)
.then(value => {
// Here, TypeScript infers `value` is of type `number`
});
fn('log', 'message')
.then(value => {
// Here, TypeScript infers `avlue` is of type `void`
});
}
If you're going to write functions for that, it may help to have some helper types:
type AddParams = ['add', number, number];
type LogParams = ['log', string];
type ActionParams =
| AddParams
| LogParams;
type Call = {
(...args: AddParams): Promise<number>;
(...args: LogParams): Promise<void>;
};
Then for instance:
const call: Call = (...args: ActionParams): Promise<any> => {
// (Dummy implementation)
if (args[0] === 'add') {
return Promise.resolve(args[1] + args[2]);
}
return Promise.resolve();
};
If you just want to write an overloaded function, you don't need the Call
type (you probably know that):
type AddAction = ['add', number, number];
type LogAction = ['log', string];
type Actions =
| AddAction
| LogAction;
function call(...args: AddAction): Promise<number>;
function call(...args: LogAction): Promise<void>;
function call(...args: Actions): Promise<any> {
// ...implementation...
}
call('add', 1, 2)
.then(value => {
// Here, TypeScript infers `value` is of type `number`
});
call('log', 'message')
.then(value => {
// Here, TypeScript infers `avlue` is of type `void`
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.