I am trying to form a comma separated string from an object,
const data = {"Ticket-1.pdf":"8e6e8255-a6e9-4626-9606-4cd255055f71.pdf","Ticket-2.pdf":"106c3613-d976-4331-ab0c-d581576e7ca1.pdf"};
const values = Object.values(data).map(x => x.substr(0, x.length - 4));
const commaJoinedValues = values.join(',');
console.log(commaJoinedValues);
How to do this with TypeScript?
getting an error file:
severity: 'Error'
message: 'Property 'values' does not exist on type 'ObjectConstructor'.'
at: '216,27'
source: 'ts'
Instead of
use
In your example case:
This will hide the ts compiler error.
Object.values()
is part of ES2017, and the compile error you are getting is because you need to configure TS to use the ES2017 library. You are probably using ES6 or ES5 library in your current TS configuration.Solution: use
es2017
ores2017.object
in your--lib
compiler option.For example, using
tsconfig.json
:Note that targeting ES2017 with TypeScript does not emit polyfills in the browser for ES2017 (meaning the above solves your compile error, but you can still encounter a runtime error because the browser doesn't implement ES2017
Object.values
), it's up to you to polyfill your project code yourself if you want. And sinceObject.values
is not yet well supported by all browsers (at the time of this writing) you definitely want a polyfill:core-js
will do the job.I have increased target in my
tsconfig.json
to enable this feature in TypeScript