Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Pappa opened this issue Feb 27, 2017 · 17 comments

Comments

@Pappa
Copy link

Pappa commented Feb 27, 2017

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.

@kwonoj
Copy link
Member

kwonoj commented Feb 27, 2017

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.

@Pappa
Copy link
Author

Pappa commented Feb 28, 2017

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

@Pappa Pappa closed this as completed Feb 28, 2017
@thomas3577
Copy link

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.

@thomas3577
Copy link

Here my Repo to reproduce

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

Just run npm start

@ds82
Copy link

ds82 commented Jul 21, 2017

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.

@ds82
Copy link

ds82 commented Jul 21, 2017

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

@prh7
Copy link

prh7 commented Jul 21, 2017

@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?

@ds82
Copy link

ds82 commented Jul 21, 2017

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

@prh7
Copy link

prh7 commented Jul 21, 2017

@ds82 Anything to be included in tsconfig.json ?

@ds82
Copy link

ds82 commented Jul 21, 2017

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

@ndraiman
Copy link

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.

@LukasBombach
Copy link

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

@salmazov
Copy link

What does it mean? I have the same error.

@tje3d
Copy link

tje3d commented Mar 10, 2018

Same error in reactxp

Solved:

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

@steph643
Copy link

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

@kakins
Copy link

kakins commented Apr 7, 2018

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 :(

@lock
Copy link

lock bot commented Jun 5, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests