Yakov Fain
Yakov Fain

Reputation: 12376

Compiling to ES6 works but fails with error TS2693 with ES5

If I compile the following code with --t ES5, it gives me an error TS2693: 'Promise' only refers to a type, but is being used as a value here. When I use --t ES6 it compiles fine. Is this a bug in the compiler (I use TypeScript 2.4.2)?

import {Subject} from 'rxjs/Subject';

enum Action{
    buy = 'BUY',
    sell = 'SELL'
}

class Order{
    constructor(public orderId: number, public traderId: number,
                public stock: string, public shares: number, public action:Action){}
}

class Trader {
    orders: Subject<Order> = new Subject<Order>();

    constructor(private traderId:number, private traderName:string){}

    placeOrder(order: Order){
        this.orders.next(order);
        console.log(`Placing order to ${order.action} ${order.shares} shares of ${order.stock}`);
    }
}

let trader: Trader = new Trader(1, 'Joe');
let order1:Order = new Order(1, 1,'IBM',100,Action.buy);

trader.placeOrder( order1);

Upvotes: 0

Views: 445

Answers (1)

Saravana
Saravana

Reputation: 40594

Promises were introduced as part of ES6. So TypeScript is correct in not knowing what a Promise is when targeting ES5.

If you want to target ES5 and still use Promises, you can add the following in your tsconfig.json:

"compilerOptions": {
    // ...
    "target": "es5",
    "lib": [
        "es2015.promise" // Or "es2015" or "es6" should work as well
    ]
}

If you are passing configuration via the command line, the option is --lib. See compiler options.

Upvotes: 1

Related Questions