【发布时间】:2017-09-25 02:17:31
【问题描述】:
我不明白为什么 boolean 的类型是 boolean[] ...
const obj: { [key: number]: string } = { 1: "Hi", 2: ", there" };
const booleans = _.chain(obj)
.keys()
.map((key: number) => ({ ItemKey: key, ItemText: obj[key] }))
.value();
有什么线索吗?!以下代码生成具有正确类型的变量 { ItemKey: number, ItemText: string }[]:
const obj: { [key: number]: string } = { 1: "Hi", 2: ", there" };
const correctType = _.chain(obj)
.keys()
.map(key => ({ ItemKey: parseInt(key), ItemText: obj[parseInt(key)] })) // key is inferred to be string, because keys() returns string[]
.value();
我正在使用 Typescript 2.0.10 进行编译,但即使是带有 Typescript 2.2.2 的 VS Code 与智能感知也是一样的。
【问题讨论】:
-
可能是 lodash 类型的问题。如果将密钥更改为
string:map((key: string) => ({ ItemKey: key, ItemText: obj[key] })),则类型推断正确。
标签: typescript lodash type-inference