155
votes

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.

22
No return value is needed for the callback function passed in to the Promise constructor. Just get rid of return.Pointy
Do 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.kalyanvgopal
Yes. JavaScript doesn't care whether you return a value or not, but it won't pay attention to it. TypeScript, however, does care.Pointy
Got it. But why does tsc fails to compile any flavour of Promose.resolve or Promise.reject?kalyanvgopal
That, I don't know. How exactly is responsePromise declared?Pointy

22 Answers

126
votes

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

Encounter the same error today and solved it with:

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

Update:

add:

import {Promise} from 'es6-promise'
37
votes

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

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

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

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 )

8
votes

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

6
votes

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

5
votes

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

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

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

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"
2
votes

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.

1
votes

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

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.

1
votes

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

File: tsconfig.json

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

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

0
votes

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

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

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.

0
votes

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;
0
votes

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.

0
votes

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