34

In the following I am converting an enum in to an array, it seems that I may have something missing in my tsconfig.json.

This is the script:

const menuItems = Object.values(MediaListFilterType).map(value => ({
    type: value,
    description: () => {
        switch (value) {
            case value === MediaListFilterType.notPitched:
                return 'Exclude already pitched';
                break;
            case value === MediaListFilterType.notDoublePitched:
                return 'Exclude double pitched';
                break;
            case value === MediaListFilterType.assignedToMe:
                return 'Assigned to me';
                break;
            case value === MediaListFilterType.notAssigned:
                return 'Unassigned';
                break;
        }
    }
}));

this gives me this error: Property 'values' does not exist on type 'ObjectConstructor'.

and the tsconfig is as follows...

{
  "compilerOptions": {
    "module": "es6",
    "target": "es2015",
    "sourceMap": true,
    "jsx": "react",
    "moduleResolution": "node",
    "declaration": false,
    "allowSyntheticDefaultImports": true
  }
}

I am a bit new at this so I am not sure what I need to change in the tsconfig. Help!

Sandra Willford
  • 3,459
  • 11
  • 49
  • 96
  • Related: https://stackoverflow.com/questions/45422573/property-entries-does-not-exist-on-type-objectconstructor/45422950#45422950 – Aluan Haddad Sep 25 '20 at 15:09

4 Answers4

42

add es2017.object to the compilerOptions.lib array in your tsconfig.json.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Charles.xue
  • 531
  • 1
  • 3
  • 4
23

You have "target": "es2015", but Object.values is not a part of es2015. Its a part of es2017 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

Fix

Use "target": "ESNext" which has that feature.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 21
    -1 Target is for specifying the standard of the compiled javascript. Using `values` is a not a syntactic change. This should be configured using `lib`, specifically `es2017.object` – CervEd Feb 19 '20 at 16:47
6

You are calling Object.values(...) while targeting es2015 (also known as ES6). Unfortunately this feature first appears in es2017 (also known as ES8), as you can see in the specifications for ES6, ES7, ES8 (first appearance of Object.values) or the MDN article.

The compiler adjusts itself to the target and reports to you that it doesn't know the function (in that version).

You may use a polyfill (as mentioned in the MDN article) or change TypeScript's target to es2017 or esnext. If you want to let it run in a browser environment, you might have to transpile that to a lower ECMAScript version like your original target es2015. This can be accomplished with Babel. There are many resources on the web which show you the details (and even more lately, as Babel 7 has been released with better support for TypeScript).

By the way, this question is very similar to this ones:

Hero Wanders
  • 3,237
  • 1
  • 10
  • 14
1

for those folks who encountered this issue on their NodeJS Application, just hit npm install on your project, possible there is no @types/node or node installed on your app

Marvin
  • 647
  • 7
  • 15