【发布时间】:2021-09-23 15:09:34
【问题描述】:
我有以下代码:
const arr = [1, 2, 3] as const;
type Keys = keyof typeof arr;
type T = {
[K in Keys]: K extends 1 ? 2 : 3 // dummy
}
为什么T 类型包含prototype keys:
在这种情况下,我希望密钥只有 0、1、2。如何只获取数组键?
我们如何通过遍历数组的键来创建一个新类型?
【问题讨论】:
-
如果这个答案有帮助请告诉我stackoverflow.com/questions/69216315/…
-
实际上元组中没有
number索引。所有索引都被字符串化 -
看这个:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…:
Any other value, including a number, is coerced to a string. This outputs 'value', since 1 is coerced into '1'
标签: typescript