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 transpilation issue with Array -- downlevelIteration usage? #659

Closed
ndrbrt opened this issue Apr 1, 2020 · 3 comments
Closed

Spread transpilation issue with Array -- downlevelIteration usage? #659

ndrbrt opened this issue Apr 1, 2020 · 3 comments
Labels
problem: stale Issue has not been responded to in some time solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: downlevelIteration Related to tsconfig downlevelIteration

Comments

@ndrbrt
Copy link

ndrbrt commented Apr 1, 2020

Current Behavior

Suppose we have this code:

const A: Array<number> = [
    ...[...Array(10)].map(() => 0),
    ...[...Array(5)].map(() => 1)
]

const B: Array<number> = []
for (let i = 0; i < 10; i++) {
    B.push(0)
}
for (let i = 0; i < 5; i++) {
    B.push(1)
}

console.log('A:', A)
console.log('B:', B)

If I run the code within a test with yarn test (i.e. tsdx test), everything is fine and the output is:

A: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ]
B: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ]

If I build the library and import it in node, though, I get something different:

A: [ <15 empty items> ]
B: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ]

Expected behavior

I'd expect the built library imported in node to behave as it does in the tests:

A: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ]
B: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ]

Additional context

  • I tried to build the reported code (put in a single file) with tsc and it works just fine (tsc test.ts and then node test.js)
  • I use the spread operator in other parts of the project (yet with a simpler syntax, e.g. arr3 = [...arr1, ...arr2]) and it works

Your environment

Software Version(s)
TSDX 0.13.0
TypeScript 3.8.3
Browser -
npm/Yarn Yarn
Node v10.15.0
Operating System macOS 10.14.6
@agilgur5
Copy link
Collaborator

agilgur5 commented Apr 1, 2020

I'll have to look into it later, but it sounds like this might be related to TypeScript's downlevelIteration, for which there's this issue: #376 .
That should behave the same between tsc and tsdx though 🤔

@ndrbrt
Copy link
Author

ndrbrt commented Apr 2, 2020

Yes thank you, I did see #376, even though actually I solved my own issue by just using "option B".

I opened this one though, because, as you pointed out, I find it strange the inconsistency between tsc and tsdx

@agilgur5
Copy link
Collaborator

agilgur5 commented Apr 3, 2020

I think it might be that the tsconfig target matters to TS for this transform; per the docs, it sounds like it only does something when targeting ES5 or below.

TSDX ignores your setting and forces target to ESNext and transpiles the rest down with @babel/preset-env, so that's probably the difference unless you're not using target?

Per #376 (comment), it sounds like adding @babel/preset-env with loose: false to your babelrc should make it transpile down

Also if you're transpiling to Node (and not browser), there's a relevant TSDX flag mentioned in that issue too -- tsdx build --target node

@agilgur5 agilgur5 changed the title Problem with the the spread operator (after the build) Spread transpilation issue with Array -- downlevelIteration issue Apr 3, 2020
@agilgur5 agilgur5 changed the title Spread transpilation issue with Array -- downlevelIteration issue Spread transpilation issue with Array -- downlevelIteration usage? Apr 3, 2020
@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists problem: stale Issue has not been responded to in some time labels Apr 11, 2020
@agilgur5 agilgur5 added the solution: workaround available There is a workaround available for this issue label Jul 16, 2020
@agilgur5 agilgur5 added the topic: downlevelIteration Related to tsconfig downlevelIteration label Aug 23, 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
problem: stale Issue has not been responded to in some time solution: duplicate This issue or pull request already exists 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

2 participants