前置的不定量参数

作者:xie392地址:https://v.douyin.com/ieQG2HdD/更新时间:2024-12-21

案例代码

1
type JSTypeMap = {
2
string: string
3
number: number
4
boolean: boolean
5
object: object
6
function: Function
7
symbol: symbol
8
undefined: undefined
9
bigint: bigint
10
}
11
12
type JSTYPENAME = keyof JSTypeMap
13
14
type ArgsType<T extends JSTYPENAME[]> = {
15
[K in keyof T] : JSTypeMap[T[K]]
16
}
17
18
19
declare function addImpl<T extends JSTYPENAME[]>(...args:[...T,(...args:ArgsType<T>)=>any]):void
20
21
// addImpl('hello','world',()=>{}) // error: 不能将类型“"hello"”分配给类型“keyof JSTypeMap”。
22
// addImpl(1,2,3,()=>{}) // error: 不能将类型“1”分配给类型“keyof JSTypeMap”
23
addImpl('number',(a)=>{}) // true