[javascript] typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here

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.

This question is related to javascript typescript promise

The answer is


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"
  ]
}

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.


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;

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


The same error here. I fixed this, using "module": "ES6" in tsconfig.


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


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';

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"]
}

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
}
}

Encounter the same error today and solved it with:

npm i --save-dev  @types/es6-promise

Update:

add:

import {Promise} from 'es6-promise'

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.`

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.


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"

I had the same error and I fixed it with this configuration:

File: tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",                      
    "module": "commonjs",                    
    "strict": true,                          
    "esModuleInterop": true                  
  }
}

I solved this by adding below code to tsconfig.json file.

"lib": [
    "ES5",
    "ES2015",
    "DOM",
    "ScriptHost"]

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.


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


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.


Finally tsc started working without any errors. But multiple changes. Thanks to Sandro Keil, Pointy & unional

  • Removed dt~aws-lambda
  • Removed options like noEmit,declaration
  • Modified Gruntfile and removed ignoreSettings

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"
                }
            },
...

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 )


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"
    ]
}

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/**/*
    ]
}

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to typescript

TS1086: An accessor cannot be declared in ambient context Element implicitly has an 'any' type because expression of type 'string' can't be used to index Angular @ViewChild() error: Expected 2 arguments, but got 1 Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } Understanding esModuleInterop in tsconfig file How can I solve the error 'TS2532: Object is possibly 'undefined'? Typescript: Type 'string | undefined' is not assignable to type 'string' Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740] Can't perform a React state update on an unmounted component TypeScript and React - children type?

Examples related to promise

Axios handling errors typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here Syntax for async arrow function Angular 2: How to call a function after get a response from subscribe http.post How to use fetch in typescript Returning Promises from Vuex actions Use async await with Array.map Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai using setTimeout on promise chain Why is my asynchronous function returning Promise { <pending> } instead of a value?