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.json
file, if thetarget
property (undercompilerOptions
) is set toes2015
or later (as suggested in the error message).{ "compilerOptions": { "target": "es2015", } }
-
In
tsconfig.json
file, addlib
property (undercompilerOptions
), and set it values toes2015
or 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 thePromise
as a variable with typeany
.declare var Promise: any;
-
Try to install
@types/node
fromnpm
;npm instal @types/node
-
Please be aware that if you are running the
tsc
command with a file name, then the compiler will ignore thetsconfig.json
file. For example, transpiling the file like this:tsc myfile.ts
You can edit thetsconfig.json
to include a set of files withfiles
property, 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