TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Array.from and other new methods from ES6

Open mayacode opened this issue 10 years ago • 4 comments

So I was googling and asking on StackOverflow. For now no clear answer. So decided to ask here as for me it is a blocker.

How can use the newest methods for existing JS types (like Array, Number)?

Array.from(someIterable) throws an error: error TS2339: Property 'from' does not exist on type 'ArrayConstructor'

I would appreciate any tip, how to make it work.

mayacode avatar Apr 19 '16 13:04 mayacode

You have to specify "target": "es6" in tsconfig.json

Alternatively you can add make .d.ts file contains

interface ObjectConstructor {
  assign(target : any, ...sources: any[]) : Object;
}

and then reference it. But if you will want to support es5, you will have to use polyfill for that - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill

MRokas avatar Apr 19 '16 15:04 MRokas

"target": "es6" in tsconfig.json, or --target es6 on commandline

RyanCavanaugh avatar Apr 19 '16 17:04 RyanCavanaugh

So, yes I changed the target from es5 to es6. And now I get: ERROR in node_modules/typescript/lib/lib.es6.d.ts (1257,11): error TS2300: Duplicate identifier 'Promise'.

mayacode avatar Apr 20 '16 06:04 mayacode

@mayacode that is because your project is importing something else that is declaring Promise (something like maybe Bluebird)

Even though your googling and questions on StackOverflow didn't resolve your issues, they are the correct places for these types of questions.

kitsonk avatar Apr 20 '16 09:04 kitsonk