【发布时间】:2019-07-12 06:35:25
【问题描述】:
我想做如下的事情:
enum Opts {
One = 'one',
Two = 'two',
Three = 'three',
}
interface OneOpts {
foo: string;
bar: string;
}
interface TwoOpts {
one: string;
two: string;
}
interface ThreeOpts {
a: string;
b: string;
}
interface FunctionResponse<T> {
id: string;
href: string;
options: T;
}
type StartArgs =
| [Opts.One, OneOpts]
| [Opts.Two, TwoOpts]
| [Opts.Three, ThreeOpts]
type FunctionReturn = FunctionResponse<OneOpts | TwoOpts | ThreeOpts>
const start = async (...args: StartArgs): Promise<FunctionReturn> => {
const [ first, second ] = args
//...
const results: FunctionReturn = await new Promise(() => {
//...
})
return results
}
// Current:
start(Opts.One, { foo: 'string', bar: 'another' })
.then((result: TwoOpts) => { // passed :(
//...
})
// Desired result:
start(Opts.One, { foo: 'string', bar: 'another' })
.then((result: TwoOpts) => { // ERROR
//...
})
具体来说,我想根据发送给它的一组输入参数来推断函数的返回类型。在 TypeScript 中执行此操作的好方法是什么?
我能够通过StartArgs 类型正确关联输入参数。但是,如果我尝试通过以下方式为整体功能执行此操作:
type myFunc =
| (args: [type, type2]): type3
| (args: [type, type2]): type3
| (args: [type, type2]): type3
在这种情况下,编译器会抱怨 ...args 会自动转换为 any[]。
【问题讨论】:
标签: typescript