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

Created on 27 Feb 2017  路  17Comments  路  Source: ReactiveX/rxjs

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.

question

Most helpful comment

Same error in reactxp

Solved:

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

All 17 comments

While I can't try this on my end, some things caught my eyes like including lib.es6.d.ts manually etcs. It'd be easier if you have repo can provide reproduce this issue.

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

If I transpile my Typescript Project (a NodeJS-Application), then I get exactly the same two errors. I use commonjs modules in tsconfig.json

node_modules/rxjs/Observable.d.ts(1,25): error TS2307: Cannot find module 'promise'.
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.

Here my Repo to reproduce

https://github.com/thomas3577/rxjs-node-typescript

Just run npm start

Can we please reopen this issue? It's still an issue .. @thomas3577 repo demonstrates it quite well.

Changing the target to ES2015 is not an option because I need node@4 support.

Fixed by adding bluebird (as Promise lib) & core-js (as common ES6 polyfill). Use @types/core-js <= 0.9.36 to avoid other errors.

@ds82 Even I am facing the same issues with rxjs. Could you please help how did you integrate bluebird and corejs libraries within the code?

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

npm i --save-dev @types/bluebird @types/[email protected]

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

@ds82 Anything to be included in tsconfig.json ?

Nope.
You can see it working in this project: https://github.com/ds82/openhab1-rest

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.

FWIW, I have this same error on my own project and I have identified this is because my repo is a monorepo

What does it mean? I have the same error.

Same error in reactxp

Solved:

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

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

I'm using Visual Studio with an Angular CLI-based project and experienced Typescript intellisense errors referring to this issue and several others. In my case, I need to still target older browsers (which apparently is what the "es5" setting is for).

Setting the "target" to "es2015" did not allow me to run the app in IE, for instance.

This is what I had to do for es2017:

"compilerOptions": {
    "target" : "es5",
    "lib": ["es5", "es2017", "dom"]
}

It got the errors in visual studio to go away. So far the app is running fine in Chrome and IE.

EDIT

After seeing the errors go away, they showed up again today :(

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LittleFox94 picture LittleFox94  路  3Comments

shenlin192 picture shenlin192  路  3Comments

benlesh picture benlesh  路  3Comments

benlesh picture benlesh  路  3Comments

cartant picture cartant  路  3Comments