2

It appears it does not work in Stackblitz, but can Object.values() be used in general in Angular projects?

IIUC Angular includes CoreJS and the purpose of it is to allow things like Object.values() to be available across all browsers?

Ole
  • 41,793
  • 59
  • 191
  • 359

2 Answers2

2

In order for typescript to recognize Object.values, the developer should add to tsconfig.json compilerOptions: {lib: ["esnext"]}. Stackblitz simply does not have this setting.

Angular absolutely can utilize this feature, because it is well supported by modern browsers (except old IE, as always)

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • Yes Stackblitz is slightly bipolar in this scenario. It supports it with general typescript, but not with angular ... – Ole Sep 17 '18 at 23:02
  • 1
    It is maybe because IIRC angular has pretty complex system of tsconfig.json files (there are few of them, one root and several extending children), and complex build flow. P.S. Thanks for selecting my answer as best. – Nurbol Alpysbayev Sep 17 '18 at 23:05
  • Thanks for clarifying it - I assume polyfills are covered by CoreJS in general right? – Ole Sep 17 '18 at 23:08
  • Sorry, I am not familiar with CoreJS, and didn't encounter it anywhere while working on Angular or Typescript projects. I can just tell you that `Object.values` as well as many new methods like `entries` is ok to use (even without any polyfills), unless supporting IE is a strong business requirement. – Nurbol Alpysbayev Sep 17 '18 at 23:11
1

Depends on which browser you're targetting. It won't work as such in non-supported browsers. You can find a list of Supporting Browsers here

That being said, you can still use it in Older Browsers by using Polyfills. You can find these polyfills here.

Polyfills are used in place of JS that is not available as a part of a Browser. If lib from tsconfig is used, it is going to compile/transpile the TypeScript into a suitable version of JavaScript specified in the lib array. So adding lib: "es2017" to tsconfig would mean it won't work on browsers that don't implicitly support ES2017.

But adding a Polyfill instead would mean that it would also work on browsers that don't support this script.

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
  • I amended the question a bit. In light of that (CoreJS) is the tsconfig `lib: "es2017"` change still necessary / Do I understand the purpose of CoreJS correctly - I thought that was the polyfill? – Ole Sep 17 '18 at 21:06
  • From what I understand, Polyfills are used in place of JS that is not available as a part of a Browser. If `lib` from `tsconfig` is used, it is going to `compile`/`transpile` the typescript into a suitable version of JavaScript specified in the `lib` array. So adding `lib: "es2017"` to `tsconfig` would mean it won't work on browsers that don't implicitly supoort ES2017. – SiddAjmera Sep 17 '18 at 21:17
  • I understand what you are saying. My original understanding though was that Angular includes CoreJS such that we don't have to worry about the polyfill at all, so I'm curious as to whether we actually need to do something to get `Object.values()` to work. It seems all Angular projects should just set `lib:esnext` by default, since Angular bakes it in via CoreJS? – Ole Sep 17 '18 at 22:57