使用时Object.keys(obj)
,返回值为a string[]
,而我想要一个(keyof obj)[]
。
const v = {
a: 1,
b: 2
}
Object.keys(v).reduce((accumulator, current) => {
accumulator.push(v[current]);
return accumulator;
}, []);
Run Code Online (Sandbox Code Playgroud)
我有错误:
元素隐式地具有“ any”类型,因为类型为“ {{a:number; b:数字;}'没有索引签名。
带有TypeScript 3.1 strict: true
。游乐场:在这里,请选中所有复选框以Options
将其激活strict: true
。
我有一个常量字符串数组,例如
const emojis = ['', '', '', '', ''] as const
Run Code Online (Sandbox Code Playgroud)
我想要一个包含该数组索引的并集的类型,例如
type emojiIndexes = IndexesOfArray<typeof emojis> // => 0 | 1 | 2 | 3 | 4
Run Code Online (Sandbox Code Playgroud)
所以我不允许使用number
并且仅使用数组中索引的确切数量
如果数组大小例如
// changed from this
// const emojis = ['', '', '', '', ''] as const
// to this
const emojis = ['', '', ''] as const // removed 2 emojis
Run Code Online (Sandbox Code Playgroud)
比,IndexesOfArray<typeof emojis>
会是0 | 1 | 2
我怎样才能创建IndexesOfArray
一个带有常量数组索引的联合类型?