jaredpalmer / tsdx

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

Cannot destructure generator functions #966

Closed rockettomatooo closed 3 years ago

rockettomatooo commented 3 years ago

Current Behavior

For testing purposes, I added this little generator function to my code.

function* counter(limit: number): Iterable<number> {
    for (let i = 0; i < limit; i++) {
        yield i + 1
    }
}

I then tried to destruct the resulting generator as an array like:

const [test] = counter(1)
console.log(test) // logs "undefined"

Expected behavior

I expected console.log(test) to log 1 since that is, what the function yields. To me, this looks like a transpilling error but I haven't found anything I could configure or change to make it work. I used the same tsconfig.json (pasted below) in a project created by create-react-app and it works fine there.

Reproduction

You can reproduce this behavior by creating a new tsdx lib and simply pasting the 2 snippets above into the src/index.tsx file building it and running the examples.

Your environment

my tsconfig.json:

{
  "include": ["src", "types"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
        "src/*": ["src/*"]
    },
    "module": "esnext",
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "downlevelIteration": true,
    "jsx": "react",
    "sourceMap": true,
    "noFallthroughCasesInSwitch": true
  }
}
  System:
    OS: macOS 11.1
    CPU: (8) x64 Apple M1
    Memory: 89.89 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.5.1 - ~/.nvm/versions/node/v15.5.1/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 7.3.0 - ~/.nvm/versions/node/v15.5.1/bin/npm
  Browsers:
    Chrome: 88.0.4324.96
    Firefox: 84.0.1
    Safari: 14.0.2
  npmPackages:
    tsdx: ^0.14.1 => 0.14.1 
    typescript: 3.7.5 => 3.7.5
rockettomatooo commented 3 years ago

@agilgur5 I see you added the duplicate-label. Could you maybe drop a link to what this issue duplicates and if there's a solution then? 🙈 I haven't found anything in that regard unfortunately 😅

Thanks in advance!

agilgur5 commented 3 years ago

@rockettomatooo yea sorry, I just haven't had time to properly respond and write a full response (and find the exact issue to reference). If you check the the downlevelIteration label, it's described well in those issues and there are directions on how to substitute that (but in one I have a better description than others).

I'm fairly sure that's the issue, but I also haven't had the time to confirm that.

agilgur5 commented 3 years ago

Per my last comment, if you check the downlevelIteration label, it has workarounds and next steps listed there. https://github.com/formium/tsdx/issues/915#issuecomment-718896740 summarizes and links out to a few specific comments.

Those are all array specific, not quite generators / iterables, but I believe they're both impacted by downlevelIteration, so the same workarounds should work.

If those don't work, we can re-open this issue.