Vscode: Promise static method give error

Created on 4 Mar 2017  路  6Comments  路  Source: microsoft/vscode

  • VSCode Version: Code 1.10.1 (653f8733dd5a5c43d66d7168b4701f94d72b62e5, 2017-03-02T00:47:58.633Z)
  • OS Version: Linux x64 4.4.0-64-generic
  • Extensions:

|Extension|Author|Version|
|---|---|---|
|vscode-markdownlint|DavidAnson|0.6.2|
|EditorConfig|EditorConfig|0.4.0|
|sort-lines|Tyriar|1.2.0|
|vscode-eslint|dbaeumer|1.2.7|
|tslint|eg2|0.8.1|
|cpptools|ms-vscode|0.10.2|
|Spell|seanmcbreen|0.9.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.
javascript typescript upstream

Most helpful comment

Thanks @katebutenko, I also found another solution using NPM modules, the following seems to work.

npm i -D @types/core-js
npm i -D core-js

This works fine for TS code that is compiled to ES5 or earlier. As soon as you compile to ES6 or newer, you will see build errors like below about duplicate types.

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": "es6",

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

Error if you add types @types/core-js using modules and try to compile to different targets( es2015, es2017).

node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate identifier 'PropertyKey'.
node_modules/@types/core-js/index.d.ts(85,5): error TS2687: All declarations of 'name' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(145,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.unscopables]' must be of type '{ copyWi
thin: boolean; entries: boolean; fill: boolean; find: boolean; findIndex: boolean; keys: ...', but here has type 'any'.
node_modules/@types/core-js/index.d.ts(262,5): error TS2687: All declarations of 'flags' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(276,5): error TS2687: All declarations of 'EPSILON' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(311,5): error TS2687: All declarations of 'MAX_SAFE_INTEGER' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(318,5): error TS2687: All declarations of 'MIN_SAFE_INTEGER' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(457,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Symbol"
', but here has type 'string'.
node_modules/@types/core-js/index.d.ts(457,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(464,5): error TS2687: All declarations of 'prototype' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(492,5): error TS2687: All declarations of 'hasInstance' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(498,5): error TS2687: All declarations of 'isConcatSpreadable' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(504,5): error TS2687: All declarations of 'iterator' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(510,5): error TS2687: All declarations of 'match' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(516,5): error TS2687: All declarations of 'replace' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(522,5): error TS2687: All declarations of 'search' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(528,5): error TS2687: All declarations of 'species' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(534,5): error TS2687: All declarations of 'split' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(540,5): error TS2687: All declarations of 'toPrimitive' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(546,5): error TS2687: All declarations of 'toStringTag' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(552,5): error TS2687: All declarations of 'unscopables' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(609,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Math"',
 but here has type 'string'.
node_modules/@types/core-js/index.d.ts(609,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(613,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"JSON"',
 but here has type 'string'.
node_modules/@types/core-js/index.d.ts(613,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.

All 6 comments

I've just been fighting with the same, and adding core-js to tsconfig.json's compilerOptions fixed it:

  "compilerOptions": {
    ...
    "types" : [ "node", "jasmine", "core-js" ]
  }

Thanks @katebutenko, I also found another solution using NPM modules, the following seems to work.

npm i -D @types/core-js
npm i -D core-js

This works fine for TS code that is compiled to ES5 or earlier. As soon as you compile to ES6 or newer, you will see build errors like below about duplicate types.

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": "es6",

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

Error if you add types @types/core-js using modules and try to compile to different targets( es2015, es2017).

node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate identifier 'PropertyKey'.
node_modules/@types/core-js/index.d.ts(85,5): error TS2687: All declarations of 'name' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(145,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.unscopables]' must be of type '{ copyWi
thin: boolean; entries: boolean; fill: boolean; find: boolean; findIndex: boolean; keys: ...', but here has type 'any'.
node_modules/@types/core-js/index.d.ts(262,5): error TS2687: All declarations of 'flags' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(276,5): error TS2687: All declarations of 'EPSILON' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(311,5): error TS2687: All declarations of 'MAX_SAFE_INTEGER' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(318,5): error TS2687: All declarations of 'MIN_SAFE_INTEGER' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(457,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Symbol"
', but here has type 'string'.
node_modules/@types/core-js/index.d.ts(457,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(464,5): error TS2687: All declarations of 'prototype' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(492,5): error TS2687: All declarations of 'hasInstance' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(498,5): error TS2687: All declarations of 'isConcatSpreadable' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(504,5): error TS2687: All declarations of 'iterator' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(510,5): error TS2687: All declarations of 'match' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(516,5): error TS2687: All declarations of 'replace' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(522,5): error TS2687: All declarations of 'search' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(528,5): error TS2687: All declarations of 'species' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(534,5): error TS2687: All declarations of 'split' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(540,5): error TS2687: All declarations of 'toPrimitive' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(546,5): error TS2687: All declarations of 'toStringTag' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(552,5): error TS2687: All declarations of 'unscopables' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(609,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Math"',
 but here has type 'string'.
node_modules/@types/core-js/index.d.ts(609,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.
node_modules/@types/core-js/index.d.ts(613,5): error TS2403: Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"JSON"',
 but here has type 'string'.
node_modules/@types/core-js/index.d.ts(613,5): error TS2687: All declarations of '[Symbol.toStringTag]' must have identical modifiers.

@rajinder-yadav yep, I've also installed @types/core-js through npm before that, for sure.

//sidenote

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

Agree :) I've also spent several hours yesterday with figuring out how to switch from typings to @types and related issues. I think the source of such issues is that related code grows rapidly when documentation is not so fast and even if it grows in parallel it can't cover all the cases. But let's hope for the best in the opensource world ;)
//end of sidenote

This is a TypeScript compiler/language issue. It does not seem that VSCode is the root cause. If you feel the current behavior of the compiler is incorrect or could be improved, please try raising this against TypeScript: https://github.com/Microsoft/TypeScript/issues

@mjbvz thanks I will do that.

I found a solution here.

i.e., update your tsconfig.json with this:

{
  "compilerOptions": {
    "lib": ["es2017", "dom"]
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

curtw picture curtw  路  3Comments

biij5698 picture biij5698  路  3Comments

vsccarl picture vsccarl  路  3Comments

philipgiuliani picture philipgiuliani  路  3Comments

v-pavanp picture v-pavanp  路  3Comments