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

[Bug]: TypeScript compilation throws error on correct program (type inference on array spread syntax?) #15380

Closed
1 task
cebamps opened this issue Jan 30, 2023 · 4 comments · Fixed by #15383
Closed
1 task
Assignees
Labels
area: typescript i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@cebamps
Copy link

cebamps commented Jan 30, 2023

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

const foo = () => {
  const bar = 0 ? mkList() : [];

  return [...bar];
};

function mkList(): any[] {
  return [];
}

export default {};

This REPL session also showcases the issue.

Configuration file name

babel.config.json

Configuration

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript"
  ]
}

Current and expected behavior

Current behavior: compilation fails with the following log:

$ /tmp/babel/node_modules/.bin/babel src --extensions .ts -d build
TypeError: /tmp/babel/src/index.ts: Property types[1] of TSUnionType expected node to be of a type ["TSType"] but instead got "GenericTypeAnnotation"
    at validate (/tmp/babel/node_modules/@babel/types/lib/definitions/utils.js:112:11)
    at validator (/tmp/babel/node_modules/@babel/types/lib/definitions/utils.js:88:7)
    at Object.validate (/tmp/babel/node_modules/@babel/types/lib/definitions/utils.js:188:7)
    at validateField (/tmp/babel/node_modules/@babel/types/lib/validators/validate.js:21:9)
    at validate (/tmp/babel/node_modules/@babel/types/lib/validators/validate.js:15:3)
    at validateNode (/tmp/babel/node_modules/@babel/types/lib/builders/validateNode.js:12:27)
    at tsUnionType (/tmp/babel/node_modules/@babel/types/lib/builders/generated/index.js:1757:36)
    at createTSUnionType (/tmp/babel/node_modules/@babel/types/lib/builders/typescript/createTSUnionType.js:18:39)
    at createUnionType (/tmp/babel/node_modules/@babel/traverse/lib/path/inference/util.js:24:16)
    at NodePath.ConditionalExpression (/tmp/babel/node_modules/@babel/traverse/lib/path/inference/inferers.js:118:36) {
  code: 'BABEL_TRANSFORM_ERROR'
}
error Command failed with exit code 1.

Moving the function definition for mkList to the top of the file fixes the issue. So does adding a type assertion on line 4: return [...(bar as typeof bar)];.

Setting targets to "defaults" in the config also fixes the issue.

Environment

  System:
    OS: macOS 12.5.1
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  npmPackages:
    @babel/cli: 7.20.7 => 7.20.7
    @babel/core: 7.20.12 => 7.20.12
    @babel/preset-env: 7.20.2 => 7.20.2
    @babel/preset-typescript: 7.18.6 => 7.18.6

Possible solution

No response

Additional context

This appears to be a regression between 7.17 and 7.18. See this REPL session, where I just changed the version to 7.17.

In the CLI, I can make the compilation succeed with the following package.json file for Yarn:

{
  "devDependencies": {
    "@babel/cli": "~7.17.0",
    "@babel/core": "~7.17.0",
    "@babel/preset-env": "~7.17.0",
    "@babel/preset-typescript": "~7.17.0",
    "typescript": "4.9.4"
  },
  "resolutions": {
    "@babel/types": "~7.17.0",
    "@babel/generator": "~7.17.0",
    "@babel/traverse": "~7.17.0"
  }
}
@babel-bot
Copy link
Collaborator

Hey @cebamps! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@liuxingbaoyu
Copy link
Member

liuxingbaoyu commented Jan 30, 2023

This is a legacy issue, path.getTypeAnnotation() returns [TSArrayType,GenericTypeAnnotation] for 0 ? mkList() : [].
The first is a ts type and the second is a flow type .
We have two solutions

  1. Make sure both types are ts or flow
  2. A similar solution to this one allows us to get the type of the current file [Bug]: Duplicate declaration for type and value in ts #15262

I personally tend to do both.

PS: I am surprised that we actually deduce the ts type during the conversion process. (babel-plugin-transform-spread)

@Amatewasu
Copy link

(I am also having the issue with @babel/core: 7.23.2.)

@Amatewasu
Copy link

I found a work around: replace the spread operator ([...a, ...b]) with the concat method a.concat(b).

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 16, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants