Sam*_*sta 3 javascript promise typescript
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
})
Run Code Online (Sandbox Code Playgroud)
你如何根据传递给函数的任何参数来确定 Promise 的返回值?
两种方法给你:
Call类型作为重载函数类型Call类型您想要的类型Call是重载的函数类型。你可以这样定义它:
type Call = {
(...args: ['add', number, number]): Promise<number>;
(...args: ['log', string]): Promise<void>;
};
Run Code Online (Sandbox Code Playgroud)
由于您需要将返回类型与参数列表相关联,因此该Actions类型并没有真正的帮助。
使用该类型键入的函数将执行您要求的推理:
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`
});
}
Run Code Online (Sandbox Code Playgroud)
如果您打算为此编写函数,使用一些辅助类型可能会有所帮助:
type AddParams = ['add', number, number];
type LogParams = ['log', string];
type ActionParams =
| AddParams
| LogParams;
type Call = {
(...args: AddParams): Promise<number>;
(...args: LogParams): Promise<void>;
};
Run Code Online (Sandbox Code Playgroud)
然后例如:
const call: Call = (...args: ActionParams): Promise<any> => {
// (Dummy implementation)
if (args[0] === 'add') {
return Promise.resolve(args[1] + args[2]);
}
return Promise.resolve();
};
Run Code Online (Sandbox Code Playgroud)
如果您只想编写重载函数,则不需要Call类型(您可能知道):
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`
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
279 次 |
| 最近记录: |