【问题标题】:typescript typings error when using es6-shim Promise.all()使用 es6-shim Promise.all() 时打字稿打字错误
【发布时间】:2016-12-12 08:34:48
【问题描述】:

问题
打字稿编译器似乎错误地为 Promise.all() 发出了类型错误。

特别是,似乎:

  • es6-shim 中 Promise.all() 的类型不正确
  • 或者,typescript 编译器没有正确解释这些类型

我查看了 es6-shim 中 Promise 的类型,但它们对我来说似乎是正确的。特别是,这似乎是被误解的那一行:

interface PromiseConstructor {
    all<T>(values: IterableShim<T | PromiseLike<T>>): Promise<T[]>;
}

也许可以输入:Promise&lt;PromiseLike&lt;T&gt;[]&gt;
而不是 Promise 返回一个(非 Promise)值的数组。

复制
OSX:埃尔卡皮坦 10.11.6 节点:v4.4.7
打字稿:1.8.10
打字:1.3.2

我设置了一个最小的示例,通过清除我的类型目录并运行:

typings install dt~es6-shim --save --global

然后我创建了一个测试文件,t.ts:

/// <reference path="typings/globals/es6-shim/index.d.ts" />
export function seedTestDatabase() : Promise<boolean[]> {
    type BP = Promise<boolean>
    var promises: BP[] = []
    var bp: BP = Promise.resolve(true)
    promises.push(bp)
    promises.push(Promise.resolve(true))
    var p = Promise.all(promises)
    return p
}

我是这样编译的:

tsc --module commonjs t.ts

产生错误:

t.ts(9,12): error TS2322: Type 'Promise<Promise<boolean>[]>' is not assignable to type 'Promise<boolean[]>'.  
Type 'Promise<boolean>[]' is not assignable to type 'boolean[]'.  
Type 'Promise<boolean>' is not assignable to type 'boolean'.  

注意:代码返回一个 Promise 解析为一个值数组,正如预期的那样。

【问题讨论】:

    标签: typescript es6-promise typescript-typings


    【解决方案1】:

    您使用的是哪个版本的 TS?

    似乎是那个错误:https://github.com/Microsoft/TypeScript/issues/10143

    它已在 TS 2.0 beta 中修复。

    【讨论】:

    • 打字稿:1.8.10
    • 谢谢,我更新到typescript@beta (=2.0.0),不再出现编译错误。
    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2020-08-30
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2016-08-21
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多