Typescript: Promise static method gives error

Created on 25 Mar 2017  Â·  20Comments  Â·  Source: microsoft/TypeScript

TypeScript Version: 2.2.1


Steps to Reproduce:

  1. Code used
let p = Promise.resolve([1, 2, 3]);
p.then(function (v) {
  console.log(v[0]); // 1
});
  1. Both the compiler and vscode give this error.
src/main.ts(17,9): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

Comment

I really think the TS compiler should be smart enough to figure out what "basic" types to include based off the field target from the TS configuration files, tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",

Aside: As a developer, the last thing I care about is wasting my time with stupid typing errors, the entire typing system needs to be hidden from the developer.

package.json dependencies

 "devDependencies": {
    "@types/node": "^7.0.8",
    "ava": "^0.18.2",
    "browserify": "^14.1.0",
    "core-js": "^2.4.1",
    "cross-env": "^3.2.4",
    "cross-var": "^1.0.3",
    "gazeall": "^0.2.5",
    "husky": "^0.13.2",
    "lite-server": "^2.3.0",
    "npm-run-all": "^4.0.2",
    "nyc": "^10.1.2",
    "shx": "^0.2.2",
    "tachyons": "^4.6.2",
    "tap-summary": "^3.0.1",
    "tape-run": "^3.0.0",
    "tslint": "^4.5.1",
    "typedoc": "^0.5.7",
    "typescript": "^2.2.1",
    "typescript-formatter": "^5.1.2"
  },
  "dependencies": {
    "bunyan": "^1.8.8"
  },
Question

Most helpful comment

You're targeting ES5. Promises are not part of ES5. They're part of ES6/ES2015.

To use Promises, you'll need to adjust your lib option in tsconfig (see http://www.typescriptlang.org/docs/handbook/compiler-options.html).

All 20 comments

You're targeting ES5. Promises are not part of ES5. They're part of ES6/ES2015.

To use Promises, you'll need to adjust your lib option in tsconfig (see http://www.typescriptlang.org/docs/handbook/compiler-options.html).

I've run into the same issue, even when targeting ES6. Writing an extension for Visual Studio Code and have the same problem described above.

Trying to compile this:

const p = new Promise<string>((resolve, reject) => { resolve('a string') });

with the following tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "out",
        "lib": [ "es6" ],
        "sourceMap": true,
        "rootDir": ".",
        "types": [ "node" ]
    },
    "exclude": [
        "node_modules",
        ".vscode-test"
    ]
}

results in the following compiler error:

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

I've been fighting against this for a couple days now and have no idea at all what to do. It does appear to be some kind of bug in the compiler.

TypeScript version: 2.2.1
MacOS Sierra 10.12.3
VSC: 1.10.2

@ellismarkf, if you run tsc --listFiles what lib files do you see?

@mhegazy: here are the lib files I see after running tsc --listFiles

/node_modules/typescript/lib/lib.es2015.d.ts
/node_modules/typescript/lib/lib.es5.d.ts
/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts
/node_modules/typescript/lib/lib.es2015.reflect.d.ts
/node_modules/typescript/lib/lib.es2015.proxy.d.ts
/node_modules/typescript/lib/lib.es2015.promise.d.ts
/node_modules/typescript/lib/lib.es2015.iterable.d.ts
/node_modules/typescript/lib/lib.es2015.symbol.d.ts
/node_modules/typescript/lib/lib.es2015.generator.d.ts
/node_modules/typescript/lib/lib.es2015.collection.d.ts
/node_modules/typescript/lib/lib.es2015.core.d.ts

I am unable to reproduce this locally, can you share a repro project that demonstrates the issue?

I had the same issue. Reverting to 2.2.0 solved it for me.

I am targeting ES6 and having this issue. +1

@mhegazy — turns out it was a silly VSCode misconfiguration. I had forgotten to add a VSCode compilation task, and wasn't compiling the TypeScript code as part of the preLaunchTask command in my VSCode launch config.

For others having a similar issue, make sure you have something like the following:

tasks.json

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "showOutput": "silent",
    "args": ["run", "compile", "--loglevel", "silent"],
    "isBackground": true,
    "problemMatcher": "$tsc-watch"
}

launch.json

        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${file}",
            "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
            "sourceMaps": true,
            "preLaunchTask": "npm"
        }

The "preLaunchTask": "npm" above refers to the task defined in tasks.json.

package.json

  "scripts": {
    "vscode:prepublish": "tsc -p ./",
    "compile": "tsc -watch -p ./",
    "postinstall": "node ./node_modules/vscode/bin/install",
    "test": "node ./node_modules/vscode/bin/test"
  }

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "out",
        "lib": [ "es2015" ],
        "sourceMap": true,
        "rootDir": ".",
        "types": [ "node" ]
    },
    "exclude": [
        "node_modules",
        ".vscode-test"
    ]
}

Hope that helps someone! Probably not since it was a bit of a silly oversight on my part >.<

@mhegazy - thanks for your help!

> tslint --fix --type-check --project .

Error at src/Server.ts:102:20: 'Promise' only refers to a type, but is being used as a value here.
Error at src/Server.ts:133:20: 'Promise' only refers to a type, but is being used as a value here.
Error at src/Server.ts:149:20: 'Promise' only refers to a type, but is being used as a value here.
Error at src/Server.ts:165:20: 'Promise' only refers to a type, but is being used as a value here.

my tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "dist",
        "rootDirs": [
            "src",
            "test"
        ],
        "sourceMap": true,
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "dist"
    ]
}

My code looks like:

  function myFunction(): Promise<number>{
       return new Promise((resolve,reject)=>{
          resolve(3);
       })
   }

I have the same issue if I delete my tsconfig.json file (my TS config is in my webpack configuration in the ts-loader options).

If I create an empty tsconfig.json, no more errors!

TS version 2.2.2

Try to add :
"lib": [ "es6", "dom" ]
to compilerOptions, then close the file and open again, just for refresh ...

worked for my project...

I failed to follow up, I ended up using the "lib" option, but what my intent was that TypeScript knows I specified target "es5" that needs a polyfill for promises on the browser. However I am using Node and what I found odd was that I also need to specify "dom" within lib. I would be happy with TypeScript being smarter to not have me specify the "lib" part in the configuration.

but what my intent was that TypeScript knows I specified target "es5" that needs a polyfill for promises on the browser

TypeScript does not inject polyfills into your code. The assumption is you will include these independently.

Well TS does something when I add lib: [ "es5", "dom" ], so I can use promise in Node with es5. I don't see the point of me having to do this, if I already set target to "es5". My point is make the configuration smarter. One thing most people don't like about TS is dealing with confusing configuration and typing error eating up all their time.

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy yep

ps: little joke, don't take it hard

I made it!

The problem is that Typescript doesn't add polyfills for you, so you have to add them.

npm install --save es6-promise

and then

import { Promise } from 'es6-promise';

wherever you need promises. This is my tsconfig.json

{
    "compilerOptions": {
        "module": "es6",
        "target": "es5",
        "outDir": "dist",
        "rootDir": "src",
        "sourceMap": true,
        "lib": [ "es6", "dom" ]
    },
    "include": [
        "./src/"
    ],
    "exclude": [ "node_modules" ],
    "compileOnSave": true
}

Could someone explain why the code in the following repository doesn't work? I think I've stripped everything down to the most simple version possible and I still get the Promise error. https://github.com/danielcovill/typescript-issue

edit: it appears the solution is not to include parameters as they result in tsconfig.json being ignored. If parameters must be supplied, include "--target es6".

Still having this issue; these little errors are annyoing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  Â·  3Comments

CyrusNajmabadi picture CyrusNajmabadi  Â·  3Comments

weswigham picture weswigham  Â·  3Comments

DanielRosenwasser picture DanielRosenwasser  Â·  3Comments

blendsdk picture blendsdk  Â·  3Comments