When using Promise in TypeScript code and transpiling, it generates the error:
'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later
There could be following reasons/fixes for this issue:
-
Check in
tsconfig.jsonfile, if thetargetproperty (undercompilerOptions) is set toes2015or later (as suggested in the error message).{ "compilerOptions": { "target": "es2015", } } -
In
tsconfig.jsonfile, addlibproperty (undercompilerOptions), and set it values toes2015or later.{ "compilerOptions": { "target": "es2015", "lib": ["dom", "es2015", "es5", "es6"], } } -
A quick work around for this error is just removing the type check for
Promise, rather than fixing it. Declar thePromiseas a variable with typeany.declare var Promise: any;
-
Try to install
@types/nodefromnpm;npm instal @types/node
-
Please be aware that if you are running the
tsccommand with a file name, then the compiler will ignore thetsconfig.jsonfile. For example, transpiling the file like this:tsc myfile.ts
You can edit thetsconfig.jsonto include a set of files withfilesproperty, e.g:{ "compilerOptions": { "module": "commonjs", "noImplicitAny": true, "removeComments": true, "preserveConstEnums": true, "sourceMap": true, "target": "es2015", "lib": ["dom", "es2015", "es5", "es6"], }, "files": [ "myfile.ts", "service1.ts", "common.ts", "util.ts", ] }
No comments:
Post a Comment