使用es6填充程序Promise.all()时发生typescript打字错误



问题
typescript编译器似乎错误地为Promise.all((.发出类型错误

特别是,似乎要么:

  • es6填充程序中Promise.all((的键入不正确
  • 或者,typescript编译器没有正确解释这些打字

我查看了es6垫片中Promise的打字,但它们对我来说似乎是正确的。特别是,这似乎是被误解的一行:

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

并可能决定键入:Promise<PromiseLike<T>[]>
而不是返回一个(非Promise(值数组的Promise。

复制
OSX:El Capitan 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,解析为一个值数组。

您正在使用哪个版本的TS?

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

它已在TS 2.0测试版中修复。

最新更新