rxjs: 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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (1 by maintainers)
Same error in reactxp
Solved:
To fix the ts build issue it should be enough to do this:
npm i --save-dev @types/bluebird @types/core-js@0.9.36In 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 bluebirdIt seems the cause was declaring
commonjsmodules intsconfig.jsonwhile I’m using ES6 modules.I’m still receiving this error when targeting
es5.Tried to all sorts of combinations with
libintsconfig.json’scompilerOptions. Tried adding@types/es6-promise,@types/core-jsand even tried to include the libraries themselves and not just the types.But i’m still receiving:
Thanks a million @tje3d! Notice that
"lib": ["es2015"]seems to be enough for me to fix the ‘Promise’ issue.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.