0

I'm restricted to using ES5 (and hence can't use the same solutions as other posts by upgrading to ES6) and have the following code:

let arr = [1,2,1,1,2,3,4]
let arr2 = [].concat([...new Set(arr)])
let arr3 = [...new Set(arr)]

The issue I'm getting is this:

Type 'Set<number>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher

Does anyone know how to fix this type? I've tried adding as number[] but to no avail. I'm not sure what else to do.

Note that this playground can be used to add the code to to see the error: https://playcode.io/typescript

user8758206
  • 2,106
  • 4
  • 22
  • 45
  • 2
    When I add the code to that playground I don't seem to see any errors, it might be worth you sharing the playground you've created with errors, rather than getting us assemble it and potentially do things differently. – DBS Nov 29 '22 at 10:14
  • You won't be able to use a `Set` or the spread syntax `...` if you're restricted to es5. – Nick Parsons Nov 29 '22 at 10:16
  • Apologies - the playground does not reproduce the same error. Presumably it doesnt use ES5 – user8758206 Nov 29 '22 at 10:17
  • Your link is broken. COnsider [this](https://tsplay.dev/m33abm). – captain-yossarian from Ukraine Nov 29 '22 at 10:20
  • `Set` was added in ES2015. Why are you restricted to using ES5? You are already using typescript. You can change the transpiled js to any version you want while still using the newer features. – adiga Nov 29 '22 at 10:24
  • Love the typescriptlang link. Getting an error when setting TS to ES5 and downlevelIteration though with that code. – user8758206 Nov 29 '22 at 10:25
  • Restricted as at work it fails some CI/CD tests when upgrading to ES6 – user8758206 Nov 29 '22 at 10:26
  • 1
    Then you can't use a `Set` if that's the case, so I'd imagine your error won't be relevant anymore as you'll need to change your solution to use some other ES5 compatible code that removes duplicates from an array (eg: [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/a/14438954)) – Nick Parsons Nov 29 '22 at 10:30

0 Answers0