我希望将一个类型参数T
约束为具有only个string
个键,值为类型number
.因此,有效的子类型如下:
type Yes_1 = { a: 1, b: 2 }
interface Yes_2 { a: number; b: number }
并且这些不是有效的子类型:
type No_1 = { a: number, b: string }
我希望能够做到以下几点:
function F<X extends Record<string, number>>() {}
如果我这样做了,它似乎会奏效:
F<Yes_1>()
但Yes_2
不起作用,因为它说:
F<Yes_2>()
// TS2344: Type Yes_2 does not satisfy the constraint Record<string, number>
/// Index signature for type string is missing in type Yes_2
但我不希望该类型有索引签名!我做什么好?