Skip to content

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

@cebamps

Description

@cebamps

💻

  • 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"
  }
}

Metadata

Metadata

Assignees

Labels

area: typescripti: regressionoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions