psnider
psnider

Reputation: 247

typescript typings error when using es6-shim Promise.all()

Problem
The typescript compiler appears to be incorrectly issuing a type error for Promise.all().

In particular, it appears that either:

I looked over the typings for Promise in es6-shim, but they appear to be correct to me. In particular, it seems like this is the line that is being misinterpreted:

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

and perhaps resolving to type: Promise<PromiseLike<T>[]>
instead of a Promise returning an array of (non-Promise) values.

To Reproduce
OSX: El Capitan 10.11.6 node: v4.4.7
typescript: 1.8.10
typings: 1.3.2

I set up a minimal example, by clearing my typings directory, and running:

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

Then I created a test file, 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
}

and I compiled this by:

tsc --module commonjs t.ts

which produces the error:

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'.  

Note: the code returns a Promise resolving to an array of values, as expected.

Upvotes: 3

Views: 493

Answers (1)

AlexG
AlexG

Reputation: 4065

which version of TS are you using ?

It appears to be that bug: https://github.com/Microsoft/TypeScript/issues/10143

It's fixed in TS 2.0 beta.

Upvotes: 1

Related Questions