Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Promise TS2304 TS2529

I had the following code:

function asyncFunc1(): Promise<string> {
    return new Promise<string>(x => x);
}

which produced the following error:

TS2304: cannot find name 'Promise'

So I changed it to explicitly declare 'Promise':

///<reference path="../typings/modules/bluebird/index.d.ts" />

import * as Promise from 'bluebird';

function asyncFunc1(): Promise<string> {
    return new Promise<string>(x => x);
}

and now I get the following error:

TS2529: Duplicate identifier 'Promise'. Compiler reserves name 'Promise' in top level scope of a module containing async functions

How can I solve this paradox?

like image 919
Alon Avatar asked Oct 10 '16 13:10

Alon


1 Answers

It seems TypeScript is protecting the Promise identifier. Try importing as a name other than Promise, or use a different set of typings. There are a few suggestions in this TypeScript GitHub issue.

Async / await with Bluebird promises

The issue is regarding the use of a different promise library with Async Await, but this very situation is likely why they are protecting the Promise name. Specifically, when we use Async/Await in TypeScript the compiler converts this to a Promise in the emitted code, so the compiler is trying to protect the Promise constructor from being over-written. You can always trick the compiler with this sort of thing, but be sure to ask yourself if it's the right thing to do.

like image 84
nbering Avatar answered Oct 20 '22 09:10

nbering