Solvedrxjs TypeScript compile error: 'Promise' only refers to a type, but is being used as a value here.

I'm seeing the following compile error when trying to compile a small Angular/Rx project:

$ tsc -p .
node_modules/rxjs/Observable.d.ts(69,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
node_modules/rxjs/operator/toPromise.d.ts(3,79): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

RxJS version: 5.2.0

Code to reproduce:

typings.json

{
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160726072212"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/js/",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "module": "commonjs",
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "./src/**/*",
    "./node_modules/typescript/lib/lib.es6.d.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

Angular service (edited)

import Rx from 'rxjs/Rx';
import {angular, ng} from "angular";

export class TweetEvents {
    requests = {
        getSentiment: new Rx.Subject<string>()
    }
    responses = {
        getSentimentSuccess: new Rx.Subject<Models.Sentiment>()
    }
    static $inject = [
        "$http"
    ];

    constructor(
        private $http: ng.IHttpService
    ) { 
        this.initGetSentiment();
    }

    private initGetSentiment(): void {
        this.requests.getSentiment
        .flatMap((search: string) => {
            return Rx.Observable.fromPromise(
                this.$http({
                    url: 'https://example.com'
                  })
            )
        })
        .subscribe((response) => {
            this.responses.getSentimentSuccess.next(response);
        });
    }
}

angular.module("events")
    .service("TweetEvents", TweetEvents);

I'm pretty stumped and can't find examples online of other people with seeing exactly the same issue.

18 Answers

✔️Accepted Answer

Same error in reactxp

Solved:

"compilerOptions": {
    "lib": ["es5", "es2015", "dom", "scripthost"]
}

Other Answers:

To fix the ts build issue it should be enough to do this:

npm i --save-dev @types/bluebird @types/core-js@0.9.36

In the files I use promises I also added
import * as Promise from 'bluebird';

edit: If you want to use bluebird in your code, you have to install it, too
npm i --save bluebird

It seems the cause was declaring commonjs modules in tsconfig.json while I'm using ES6 modules.

I'm still receiving this error when targeting es5.

Tried to all sorts of combinations with lib in tsconfig.json's compilerOptions.
Tried adding @types/es6-promise, @types/core-js and even tried to include the libraries themselves and not just the types.

But i'm still receiving:

ERROR in [at-loader] ./node_modules/rxjs/Observable.d.ts:58:60
    TS2693: 'Promise' only refers to a type, but is being used as a value here.

ERROR in [at-loader] ./node_modules/rxjs/Observable.d.ts:73:59
    TS2693: 'Promise' only refers to a type, but is being used as a value here.

Thanks a million @tje3d!
Notice that "lib": ["es2015"] seems to be enough for me to fix the 'Promise' issue.

More Issues: