Reputation: 1969
I am trying to use Typescript for my AWS Lambda and i am getting the following errors where ever I use promises.
error TS2693: 'Promise' only refers to a type, but is being used as a value here.
I tried using the following variations in the code
Using the Promise constructor
responsePromise = new Promise((resolve, reject) => {
return reject(new Error(`missing is needed data`))
})
using Promise.reject
responsePromise = Promise.reject(new Error(`Unsupported method "${request.httpMethod}"`));
Following are the versions in my dev dependencies:
"typescript": "^2.2.2"
"@types/aws-lambda": "0.0.9",
"@types/core-js": "^0.9.40",
"@types/node": "^7.0.12",
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
// "typeRoots" : ["./typings", "./node_modules/@types"],
"target": "es5",
// "types" : [ "core-js" ],
"noImplicitAny": true,
"strictNullChecks": true,
"allowJs": true,
"noEmit": true,
"alwaysStrict": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist",
"moduleResolution": "Node",
"declaration": true,
"lib": [
"es6"
]
},
"include": [
"index.ts",
"lib/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
I am using grunt-ts with the following configuration for running ts task.
ts: {
app: {
tsconfig: {
tsconfig: "./tsconfig.json",
ignoreSettings: true
}
},
...
I tried with the solution mentioned in I get: [ts] 'Promise' only refers to a type, but is being used as a value here but no luck.
Upvotes: 178
Views: 198491
Reputation: 528
I had the same issue. Using a class instead of a type for my nested dto works for me.
Upvotes: 0
Reputation: 1065
There are lot of answers here that do a lot of things. The trick however is already mentioned in several answers. Answering separately b/c I want to call it out specifically though.
The only change actually required to fix this is to change "target" to >= "es2015." Just like the error says... What it fails to say however, if that it won't make a damn bit of difference until you RESTART the ide :(
So
Upvotes: 0
Reputation: 4365
I got rid of this same error in index.ts
with these combined properties:
In tsconfig.json:
"compilerOptions": {
"target": "ES6"
And in package.json:
"main": "index.ts",
"scripts": {
"start": "tsc -p tsconfig.json && node index.js"
Upvotes: 12
Reputation: 940
Please be aware that if you are running the tsc command with a file name ie:
tsc testfile.ts
then the tsconfig.json compiler configuration file is ignored. The solution is to run either the tsc command on its own, in which case all .ts files in the directory will be compiled, unless you have edited the tsconfig.json to include a set of files.
see 'using the files property'... https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Upvotes: 4
Reputation: 480
I had the same error and I fixed it with this configuration:
File: tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
}
}
Upvotes: 1
Reputation: 1702
Add below line to file where error is being thrown.This should fix the issue
declare var Promise: any;
P.S: This is definitely not the optimal solution
Upvotes: 6
Reputation: 1909
Well, this might be counter-intuitive but I solved this adding esnext
to my lib
.
{
"compilerOptions": {
"lib": [
"esnext"
],
"target": "es5",
}
}
The FIX, as suggested by the compiler is to
Try changing the
lib
compiler option to es2015 or later.
Upvotes: 3
Reputation: 9564
If you're using the DefinitelyTyped repository in your project you might be experiencing this recent issue.
A decent workaround you might use (other than waiting for an updated build of the definitions file or refactoring your TS code) is to specify an explicit version+build for the core-js typings rather than let Visual Studio pick the latest/most recent one. I found one that seems to be unaffected by this problem (in my case at least), you can use it replacing the following line from your package.json file:
"scripts": {
"postinstall": "typings install dt~core-js --global"
}
With the following one:
"scripts": {
"postinstall": "typings install [email protected]+20161130133742 --global"
}
This fixed my issue for good. However, is highly recommended to remove the explicit version+build reference as soon as the issue will be released.
For further info regarding this issue, you can also read this blog post that I wrote on the topic.
Upvotes: 1
Reputation: 1658
I had this error but I resolved it by using this command, my ts file name is promises-fs.ts:
tsc promises-fs.ts --target es6 && node promises-fs.js
and the error is gone
Upvotes: 9
Reputation: 1018
I had the same issue until I added the following lib array in typeScript 3.0.1
tsconfig.json
{
"compilerOptions": {
"outDir": "lib",
"module": "commonjs",
"allowJs": false,
"declaration": true,
"target": "es5",
"lib": ["dom", "es2015", "es5", "es6"],
"rootDir": "src"
},
"include": ["./**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
Upvotes: 5
Reputation: 1098
Encounter the same error today and solved it with:
npm i --save-dev @types/es6-promise
Update:
add:
import {Promise} from 'es6-promise'
Upvotes: 94
Reputation: 2515
Here is my tip. Tested with vscode 1.21.1 (on MAC)
Put below config to tsconfig.json
"lib": [
"es2016",
"dom"
]
into compilerOptions
Restart IDE (this action is required :D )
Upvotes: 12
Reputation: 1183
Having spent lot of time trying to fix this. I had no luck with any solution provide here or elsewhere.
But then later realised it wasn't so much as just solving the issue. But you also need to RESTART the VSCODE for it to take affect.
Upvotes: 0
Reputation: 461
None of the up-voted answers here work for me. Here is a guaranteed and reasonable solution. Put this near the top of any code file that uses Promise...
declare const Promise: any;
Upvotes: 0
Reputation: 9
npm i --save-dev @types/es6-promise
after up command, you'd better check tsconfig.json make sure the "target" must great than "es6". maybe tsc not support es5 yet.
Upvotes: 0
Reputation: 559
Just change the target to "ES2017" in tsconfig.json file.
this is my tsconfig.json file
{
"compilerOptions": {
/* Basic Options */
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"strict": true /* Enable all strict type-checking options. */
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Upvotes: 0
Reputation: 934
Core-js did not work for me as it caused other issues, however, simply installing the latest version of npm i @types/es6-promise --save-dev
got rid of the issues. The issues for me stemmed from compiling an sdk that was using rxjs. Here is the error I was getting:
`node_modules/rxjs/Observable.d.ts(59,60): error TS2693: Promise only refers to a type, but is being used as a value here.`
Upvotes: 1
Reputation: 1155
Had the same issue with typescript and the aws-sdk
. The solve was to change the target to es6
.
My complete tsconfig.json
file:
{
compilerOptions: {
outDir: ./dist/,
sourceMap: true,
noImplicitAny: true,
module: commonjs,
target: es6,
jsx: react,
allowJs: true
},
include: [
./src/**/*
]
}
Upvotes: 4
Reputation: 928
I had the same problem and this saved me from the problem in second:
write in console this:
npm i --save bluebird
npm i --save-dev @types/bluebird @types/[email protected]
in the file where the problem is copy paste this:
import * as Promise from 'bluebird';
Upvotes: 0
Reputation: 2561
Solved by changing the target in compilerOptions.
{
"compilerOptions": {
"module": "es2015",
"target": "es2015",
"lib": [
"es2016",
"dom"
],
"moduleResolution": "node",
"noImplicitAny": false,
"sourceMap": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./public/js/app"
},
"exclude": [
"node_modules",
"public/js",
"assets/app/polyfills.ts"
],
"angularCompilerOptions": {
"skipMetadataEmit": true
}
}
Upvotes: 18
Reputation: 409
I solved this by adding below code to tsconfig.json file.
"lib": [
"ES5",
"ES2015",
"DOM",
"ScriptHost"]
Upvotes: 40
Reputation: 1969
Finally tsc started working without any errors. But multiple changes. Thanks to Sandro Keil, Pointy & unional
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"strictNullChecks": true,
"alwaysStrict": true,
"preserveConstEnums": true,
"sourceMap": false,
"moduleResolution": "Node",
"lib": [
"dom",
"es2015",
"es5",
"es6"
]
},
"include": [
"*",
"src/**/*"
],
"exclude": [
"./node_modules"
]
}
Gruntfile.js
ts: {
app: {
tsconfig: {
tsconfig: "./tsconfig.json"
}
},
...
Upvotes: 4
Reputation: 1427
I had the same issue with the aws-sdk
and I solved it by using "target": "es2015"
. This is my tsconfig.json
file.
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": false,
"noImplicitAny": false,
"module": "commonjs",
"target": "es2015"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
Upvotes: 136