jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.24k stars 508 forks source link

Construction of array from array destructuring is transpiled weird (downlevelIteration) #915

Closed merisbahti closed 3 years ago

merisbahti commented 3 years ago

Current Behavior

[...new Array(2)] // [undefined, undefined]

is transpiled to:

[].concat(new Array(2)) // [empty × 2]

Expected behavior

I don't know, it's just that I think that we need to transpile it so that it evaluates to [undefined, undefined]

Suggested solution(s)

No suggestions, since I don't know how to fix this. Maybe check what settings we have for babel?

Additional context

None.

Your environment

 System:
    OS: Linux 4.14 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (16) x64 Intel(R) Xeon(R) CPU @ 2.30GHz
    Memory: 20.31 GB / 58.98 GB
    Container: Yes
    Shell: 5.0.16 - /bin/bash
  Binaries:
    Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v12.19.0/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
  npmPackages:
    tsdx: ^0.14.0 => 0.14.0 
    typescript: ^4.0.2 => 4.0.2 
  npmGlobalPackages:
    typescript: 4.0.3
agilgur5 commented 3 years ago

Looks like a downlevelIteration issue. This transpilation isn't "wrong" per se, it's spec vs. non-spec; the respective Babel/TS transforms are actually intentionally written so.

Can see https://github.com/formium/tsdx/issues/760#issuecomment-659453014 and https://github.com/formium/tsdx/issues/659#issuecomment-608519036 that this seems to duplicate. To summarize, if you're creating a Node lib, you can use --target node or specify that in browserslistrc. If you're writing a browser lib, you can set preset-env to loose: false.

Per https://github.com/formium/tsdx/issues/760#issuecomment-692979270, I'm looking to add warnings to usage of tsconfig.json's downlevelIteration feature and perhaps looking to change preset-env in TSDX to not set loose by default, but it's unclear if the latter is breaking as it is for another plugin mentioned there.