您可以在TS3.1及更高版本中使用
mapped array/tuple types
. 更容易得到
tuples
推断的
rest parameters
而不是数组参数,因此我将显示:
function getVariadic<T extends Array<AnyCon>>(...cons: T): {
[K in keyof T]: T[K] extends AnyCon ? InstanceType<T[K]> : never
};
function getVariadic(...cons: AnyCon[]): any[] {
return cons.map((c: AnyCon) => new c());
}
let [t2, t3]: [T2, T3] = getVariadic(T2, T3);
我想这和你预期的一样。希望有帮助。祝你好运!
function getArray<T extends Array<AnyCon>>(cons: T): {
[K in keyof T]: T[K] extends AnyCon ? InstanceType<T[K]> : never
};
function getArray(cons: AnyCon[]): any[] {
return cons.map((c: AnyCon) => new c());
}
// error, getArray([T2, T3]) returns (T2 | T3)[]!
let [t2, t3]: [T2, T3] = getArray([T2, T3]); // ð
哦,让我们再试一次
// okay, but verbose and redundant
let [t2, t3]: [T2, T3] = getArray([T2, T3] as [typeof T2, typeof T3]); // ð
也许我们可以用一个
helper function
[T2, T3]
推断为元组类型:
// put this in a library somewhere
type Lit = string | number | boolean | undefined | null | void | {};
const tuple = <T extends Lit[]>(...args: T) => args;
// works! but now is very similar to getVariadic
let [t2, t3]: [T2, T3] = getArray(tuple(T2, T3)); ð
最后,一次
TypeScript 3.4
const
contexts
这是一个有点容易,但需要一些更多的变化
getArray()
// note the widening to ReadonlyArray
function getArray<T extends ReadonlyArray<AnyCon>>(cons: T): {
-readonly [K in keyof T]: T[K] extends AnyCon ? InstanceType<T[K]> : never
};
function getArray(cons: AnyCon[]): any[] {
return cons.map((c: AnyCon) => new c());
}
// works and is the least repetitive
let [t2, t3]: [T2, T3] = getArray([T2, T3] as const); ð
但正如我上面所说,
getVariadic()