Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise static method gives error #14858

Closed
rajinder-yadav opened this issue Mar 25, 2017 · 20 comments
Closed

Promise static method gives error #14858

rajinder-yadav opened this issue Mar 25, 2017 · 20 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@rajinder-yadav
Copy link

rajinder-yadav commented Mar 25, 2017

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"
  },
@jwbay
Copy link
Contributor

jwbay commented Mar 25, 2017

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

@ellismarkf
Copy link

ellismarkf commented Mar 27, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Mar 27, 2017

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

@ellismarkf
Copy link

@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

@mhegazy
Copy link
Contributor

mhegazy commented Mar 27, 2017

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

@DogAndHerDude
Copy link

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

@bertolo1988
Copy link

I am targeting ES6 and having this issue. +1

@ellismarkf
Copy link

ellismarkf commented Apr 4, 2017

@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!

@bertolo1988
Copy link

bertolo1988 commented Apr 4, 2017

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

@cr0cK
Copy link

cr0cK commented Apr 9, 2017

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

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Apr 27, 2017
@oshri551
Copy link

oshri551 commented May 4, 2017

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

worked for my project...

@rajinder-yadav
Copy link
Author

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.

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2017

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.

@mhegazy mhegazy added Question An issue which isn't directly actionable in code and removed Needs More Info The issue still hasn't been fully clarified labels May 5, 2017
@rajinder-yadav
Copy link
Author

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.

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

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

@mhegazy mhegazy closed this as completed May 22, 2017
@bertolo1988
Copy link

bertolo1988 commented May 23, 2017

@mhegazy yep

ps: little joke, don't take it hard

@truca
Copy link

truca commented Jul 5, 2017

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
}

@danielcovill
Copy link

danielcovill commented Sep 5, 2017

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

@HunderlineK
Copy link

Still having this issue; these little errors are annyoing

@manoharreddyporeddy
Copy link

See a solution here: https://stackoverflow.com/a/51334882/984471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests