【问题标题】:Strange behavior using Lodash and Typescript使用 Lodash 和 Typescript 的奇怪行为
【发布时间】: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


【解决方案1】:

这是 lodash 类型的问题。我会提交一个拉取请求!

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/c2bca7c/types/lodash/index.d.ts#L8563

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2018-09-16
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多