Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.entries() and Object.values() are not typed as arrays in WebStorm/PhpStorm

I have TypeScript project with ES6 target, it uses core-js to polyfill ES2017 features and tsconfig.json is configured accordingly.

When Object.entries(...) and Object.values(...) are used, the results don't have array methods and properties (map, forEach, length, etc), they appear as plain objects to IDE, so any[] type should be casted explicitly:

While Object.keys(...) behaves like it should.

At the same time IDE somehow 'knows' about the proper types for Object.entries and Object.values, they are shown in accordance with TypeScript's lib.es2017.object.d.ts on Ctrl+Shift+P. But it seems to ignore the types for inspection, because overriding ObjectConstructor in current file solves the problem:

interface ObjectConstructor {
    values(o: any): any[];
    entries(o: any): [string, any][];
}

tsc seems to be fine with the typings, so it looks like IDE-specific problem.

This happens only when Use TypeScript service in Languages & Frameworks > TypeScript is unchecked. Everything wents normal when TypeScript service is enabled (it is disabled intentionally because there were problems with TS service before).

Here is tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "strictNullChecks": false,
    "baseUrl": "./src",
    "paths": [],
    "lib": [
      "es2016",
      "es2017.object"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

What does this mean? Did my setup go wrong somewhere?

The problem persists with TypeScript 2.1.5 and latest IDE (EAP 2017.1).

like image 245
Estus Flask Avatar asked Nov 08 '22 02:11

Estus Flask


1 Answers

Typescript 2.3 introduced new support for iterators behind the --downlevel-iteration compiler flag, or by setting .compilerOptions.downlevelIteration to true in your tsconfig.json.

Note that this answer is the same as that one since it refers to the same compiler flag and similar symptoms, even though one refers to problems with a compiler and this one is about some IDE integration.

like image 185
svvac Avatar answered Nov 15 '22 12:11

svvac