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

JavascriptTypescriptPromise

Javascript Problem Overview


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 https://stackoverflow.com/questions/42984670/i-get-ts-promise-only-refers-to-a-type-but-is-being-used-as-a-value-here but no luck.

Javascript Solutions


Solution 1 - Javascript

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

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

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

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

Solution 4 - Javascript

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

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 )

Solution 6 - Javascript

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

Solution 7 - Javascript

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"

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Solution 10 - Javascript

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

Solution 11 - Javascript

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

Solution 12 - Javascript

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.

Solution 13 - Javascript

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.

Solution 14 - Javascript

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

Solution 15 - Javascript

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

File: tsconfig.json

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

Solution 16 - Javascript

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

Solution 17 - Javascript

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/core-js@0.9.36

in the file where the problem is copy paste this:

import * as Promise from 'bluebird';


Solution 18 - Javascript

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

Solution 19 - Javascript

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.

Solution 20 - Javascript

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;

Solution 21 - Javascript

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.

Solution 22 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkalyanvgopalView Question on Stackoverflow
Solution 1 - JavascriptSandro KeilView Answer on Stackoverflow
Solution 2 - JavascriptKenshinView Answer on Stackoverflow
Solution 3 - JavascriptNileshView Answer on Stackoverflow
Solution 4 - JavascriptMani SView Answer on Stackoverflow
Solution 5 - JavascriptNhan CaoView Answer on Stackoverflow
Solution 6 - JavascriptHazem HASANView Answer on Stackoverflow
Solution 7 - JavascriptWashington GuedesView Answer on Stackoverflow
Solution 8 - JavascriptImamudin NaseemView Answer on Stackoverflow
Solution 9 - JavascriptJon GearView Answer on Stackoverflow
Solution 10 - JavascriptkalyanvgopalView Answer on Stackoverflow
Solution 11 - JavascriptFanManProView Answer on Stackoverflow
Solution 12 - JavascriptSalathiel GenèseView Answer on Stackoverflow
Solution 13 - JavascriptDarksealView Answer on Stackoverflow
Solution 14 - JavascriptDanny FenstermakerView Answer on Stackoverflow
Solution 15 - JavascriptJonathan MendozaView Answer on Stackoverflow
Solution 16 - Javascriptmiller the gorillaView Answer on Stackoverflow
Solution 17 - JavascriptZack ZilicView Answer on Stackoverflow
Solution 18 - JavascriptCamilo SotoView Answer on Stackoverflow
Solution 19 - JavascriptMostoneView Answer on Stackoverflow
Solution 20 - Javascriptuser1618323View Answer on Stackoverflow
Solution 21 - Javascriptravish.hackerView Answer on Stackoverflow
Solution 22 - JavascriptKamil NajaView Answer on Stackoverflow