Skip to content

[Bug]: TSLiteralType should accept UnaryExpression #13327

@alexandre-bbx

Description

@alexandre-bbx

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

See REPL: https://runkit.com/alexandre-cai/60a2ae9feaab4400130133c8

transform(`
  type Test = 1 | -1;
`, {
  parserOpts: {
    plugins: ["typescript"],
  },
  plugins: [
    ({ types: t }) => ({
      visitor: {
        TSLiteralType(path) {
          path.node = t.tsLiteralType(path.node.literal)
        }
      }
    }),
  ],
})

Configuration file name

No response

Configuration

No response

Current and expected behavior

Babel currently accepts UnaryExpression while parsing TSLiteralType, but can’t generate one. Output:

TypeError: unknown: Property literal of TSLiteralType expected node to be of a type 
["NumericLiteral","StringLiteral","BooleanLiteral","BigIntLiteral"] but instead got "UnaryExpression"

Environment

  • @babel/core: 7.14.2

Possible solution

https://github.com/babel/babel/blob/main/packages/babel-types/src/definitions/typescript.ts#L342
Add UnaryExpression.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions