typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here
Solution 1
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"
]
}
Solution 2
Encounter the same error today and solved it with:
npm i --save-dev @types/es6-promise
Update:
add:
import {Promise} from 'es6-promise'
Solution 3
I solved this by adding below code to tsconfig.json file.
"lib": [
"ES5",
"ES2015",
"DOM",
"ScriptHost"]
Solution 4
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
}
}
Solution 5
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 )
Related videos on Youtube
kalyanvgopal
Updated on June 12, 2020Comments
-
kalyanvgopal almost 4 years
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}"`));
Versions
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",
Contents of tsconfig.json
{ "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.
-
Pointy about 7 yearsNo return value is needed for the callback function passed in to the Promise constructor. Just get rid of
return
. -
kalyanvgopal about 7 yearsDo you mean like this?
responsePromise = new Promise((resolve, reject) => { reject(new Error("missing is needed data"))})
I tried it. But it did not hep with the problem. -
Pointy about 7 yearsYes. JavaScript doesn't care whether you return a value or not, but it won't pay attention to it. TypeScript, however, does care.
-
kalyanvgopal about 7 yearsGot it. But why does tsc fails to compile any flavour of Promose.resolve or Promise.reject?
-
Pointy about 7 yearsThat, I don't know. How exactly is
responsePromise
declared? -
kalyanvgopal about 7 years@Pointy I tried to define like this.
let responsePromise: Promise<void> | Promise<any> | Promise<Error> = Promise.reject('Unknown error');
. but it did not help. -
unional about 7 years
-
Admin over 6 yearsPointy, if the function needs to return a value the 'return' keyword is definitely required. I'm running into this problem too, and it's definitely about the fact that TypeScript is having issues handling the Promise object correctly, and has nothing to do with any of the kalyanvgopal's being wrong because his code is fine.
-
johnsimer about 5 yearsto those trying any of the below answers and finding no luck, make sure your
tsconfig.json
is in the right directory! mine was in a directory below the file that was giving me the ts2693 error.
-
-
Sandro Keil about 7 yearsMaybe the
@types/aws-lambda
are out of date. Amazon ships Typescript types with the official SDK. There is no need for DefinitelyTyped. -
muzurB almost 7 yearsThat solved the exact error defined in the question for me and I was just importing rxjs, not even using Promis. Thanks so much!
-
BillyRayCyrus almost 7 yearsthis worked for me, but note that the "lib" array needs to be inside of the "compilerOptions" object in the tsconfig.json file.
-
JDTLH9 almost 7 yearsUsing TypeScript 2.4.1 I had to change all of the characters in the string array to be lowercase. Then it worked. Many thanks.
-
Admin over 6 yearsYes tartgeting es6 should fix it, but then you have less browser compatibility. Most apps are still today targeting es5, because many browsers still aren't on es6 yet (as of 2017)
-
Admin over 6 yearsThis is probably the best answer for those who were targeting es5 to begin with. Switching from es5 to es2015 also fixed this for me as well. Be warned however, you likely will still see the error until you shut down and restart your IDE/editor. Something about the TSC (or it's watch mode) was making it appear that this wasn't fixing it when it really was, but required a restart of vscode.
-
Admin over 6 yearsupdate/addendum: if you want to still target es5 (for better browser support, and is important) that does still work as long as you supply this in your compiler options: "lib" : ["es2015", "dom", "ScriptHost"], The trick for me was realizing i had to restart VSCode editor, before it would begin working after making that change.
-
edibleEnergy over 6 yearsThis is just removing the type check for 'Promise,' rather than fixing it so Typescript finds the correct type.
-
Legends over 6 years+ and restarting VS Code helps too, after installation of the types
-
Loic Coenen about 6 yearsErratum: It works using this line
import {Promise} from 'es6-promise'
; -
FanManPro about 6 years@user2080225 although that is true, it doesn't make my answer less correct since the original question didn't state anything about browser compatibility. Therefore this solution could still help others like it helped me.
-
Calin Vlasin almost 6 yearsThis workaround does not work with Internet Explorer 11 for example. I throws an 'undefined' error when trying to use Promise. Anyway with Chrome the workaround does his job.
-
bArraxas almost 6 yearswhere to add "import {Promise} from 'es6-promise'" ?
-
atconway over 5 yearsThis is listed in multiple of the answers here and it important for sure: "Restart IDE (this action is required)"
-
Hafiz Temuri about 5 yearsthat's a coward way out. Why use the
TS
if you are going to do something like this. I mean what's the point then?! -
Hafiz Temuri about 5 yearsNo, don't do this... or just don't use the typescript if you are going to do something like this
-
Thorkil Værge over 4 yearsI agree that this is not optimal but it is unfair to say that this declaration means that there is no point in using TS. Dropping typecheck on one type does not make the rest of the typechecks useless. And for me, this was the only solution that worked.
-
snarf over 4 yearsI've used this solution in the past, but it's not working for me at the moment.
import { Promise } from '../node_modules/es6-promise/es6-promise';
, however, seems to be working fine. Why would TS be unable to find the installed typings? -
Jeremy Thille over 4 yearsMay be a dirty hack, but it's the only thing that worked for me after I tried all other solutions on this page. At least I got my code compiled and working. I dig dirty hacks that work.