Object.keys在TypeScript中使用数字

29 浏览
0 Comments

Object.keys在TypeScript中使用数字

type Foo = { [key: number]: string }
const foo: Foo = { 100: 'foo', 200: 'bar' }
const sizes: number[] = Object.keys(foo)

得到的错误信息为:

类型“string[]”不能赋给类型“number[]”

为什么Typescript会假设是string[]?在这里创建一个数字数组的正确方式是什么?

0