我的变量x
定义如下:
function foo (a: string, ...nums: number[]): string {
return a;
}
const x: Parameters<typeof foo>
// Inferred: const x: [string, ...number[]]
现在,我想提取具有正确类型的字符串和数字。 对于字符串,我有:
const sExplicit: Parameters<typeof foo>[0] //easy to specify
const sInferred = x[0] //type inferred correctly
我想声明保存所有数字的变量。我该怎么做?
我尝试了以下方法:
const numsExplicit: ??? Howto specify the type
const numsInferred = x.slice(1) // incorrectly inferred as (string | number)[]
最佳答案
查找类型为 number | string
仅。
我会自己添加数组:
function foo (a: string, ...nums: number[]): string {
return a;
}
declare const x: Parameters<typeof foo>[1][];
关于typescript - 如何从 TypeScript 中的记录类型中提取 varargs 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62948624/