Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spread syntax can't work with iterables (Set, Map) -- downlevelIteration issue #376

Closed
maroon1 opened this issue Dec 17, 2019 · 9 comments
Closed
Labels
help wanted Extra attention is needed solution: workaround available There is a workaround available for this issue topic: downlevelIteration Related to tsconfig downlevelIteration

Comments

@maroon1
Copy link

maroon1 commented Dec 17, 2019

// Input
const set = new Set([1, 2, 3]);

export const spread = [...set, 4, 5];
// Output lib.esm.js
var set =
/*#__PURE__*/
new Set([1, 2, 3]);
var spread =
/*#__PURE__*/
[].concat(set, 4, 5);

export { spread };
//# sourceMappingURL=lib.esm.js.map

Current Behavior

import { spread } from 'lib';

console.log(spread);
// [Set(3), 4, 5]

Expected behavior

import { spread } from 'lib';

console.log(spread);
// [1, 2, 3, 4, 5]

Suggested solution(s)

Additional context

Your environment

Software Version(s)
TSDX 0.11.0
TypeScript 3.7.3
Browser Chrome
npm/Yarn 6.12.1
Node v13.1.0
Operating System MacOS 10.15
@swyxio swyxio added good first issue Good for newcomers help wanted Extra attention is needed labels Dec 17, 2019
@swyxio
Copy link
Collaborator

swyxio commented Dec 17, 2019

would love help finding root cause. bet we have a bad transform in here.

@ambroseus
Copy link
Contributor

ambroseus commented Dec 17, 2019

@maroon1 @sw-yx this is typescript issue
microsoft/TypeScript#8856

@ambroseus
Copy link
Contributor

ambroseus commented Dec 17, 2019

with option "downlevelIteration": true in tsconfig and tsdx build

const set = new Set([1, 2, 3])
console.log([...set, 4, 5])

// [ Set { 1, 2, 3 }, 4, 5 ]

meanwhile pure tsc --downlevelIteration

// [ 1, 2, 3, 4, 5 ]

...so need to check rollup/babel configs/presets

@ambroseus
Copy link
Contributor

related:
https://babeljs.io/docs/en/babel-plugin-transform-spread

"plugins": [
    ["@babel/plugin-transform-spread", {
      "loose": true
    }]
  ]

loose defaults to false. In loose mode true, all iterables are assumed to be arrays.

@swyxio
Copy link
Collaborator

swyxio commented Dec 17, 2019

ok. got a recommendation? should we add that flag?

@ambroseus
Copy link
Contributor

@sw-yx not sure yet.. still playing with babel options & configs
will provide recommendations soon))

@ambroseus
Copy link
Contributor

ambroseus commented Dec 17, 2019

yes, I found combination to produce correct result with spread iterables:
Screenshot from 2019-12-17 23-21-25

  1. use option "downlevelIteration": true in tsconfig + "target": "es5"
  2. option loose for @babel/prest-env must be set to false

Screenshot from 2019-12-17 22-59-55

do we need some fix? maybe no.. but need to add some instructions how to config tsdx in this case

@ambroseus
Copy link
Contributor

another solution:

  1. use option "downlevelIteration": true in tsconfig
  2. tsdx build --target node
    result - es6 as is without transpilation:
const spread = [...
/*#__PURE__*/
new Set([1, 2, 3]), 4, 5];
console.log(spread);

@maroon1
Copy link
Author

maroon1 commented Dec 18, 2019

@ambroseus thx.

@maroon1 maroon1 closed this as completed Dec 18, 2019
@ambroseus ambroseus mentioned this issue Dec 18, 2019
@agilgur5 agilgur5 removed the good first issue Good for newcomers label Mar 9, 2020
@agilgur5 agilgur5 changed the title Spread syntax can't work with iterables(Set, Map) Spread syntax can't work with iterables (Set, Map) -- downlevelIteration issue Apr 1, 2020
@agilgur5 agilgur5 added solution: workaround available There is a workaround available for this issue topic: downlevelIteration Related to tsconfig downlevelIteration labels Jul 16, 2020
@agilgur5 agilgur5 added this to the Future Non-breaking milestone Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed solution: workaround available There is a workaround available for this issue topic: downlevelIteration Related to tsconfig downlevelIteration
Projects
None yet
Development

No branches or pull requests

4 participants