Skip to content

[Bug]: TSTypeQuery ignores second argument: operator #16799

@coderaiser

Description

@coderaiser

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

console.log(t.TSTypeOperator(t.TSTypeQuery(t.Identifier('x')), 'keyof'));
image

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current behaviour: operator field is null
Expected behaviour: operator field is keyof

I want to create node like this:

type ODirection = typeof ODirection[keyof typeof ODirection];

But have only:

type ODirection = typeof ODirection[null typeof ODirection];

When using:

const id = Identifier('ODirection');
const type = TSTypeAliasDeclaration(id, null, TSIndexedAccessType(TSTypeQuery(id), TSTypeOperator(TSTypeQuery(id), 'keyof')))

Environment

latest babel

Possible solution

Add handling of operator to TSTypeQuery.

Looks like the problem in https://github.com/babel/babel/blob/main/packages/babel-types/src/builders/generated/index.ts#L2254:
image
It should be https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/type/TSTypeOperator/spec.ts:
image

Additional context

https://astexplorer.net/#/gist/5441a8fd17a10000071fc31cab269d1f/4d5a67647e7a8c7e6296e253b20c3b6c3105cd50

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions