Skip to content

Bug: AST-Spec has wrong parent types for MethodDefinition Nodes and others #9792

@SebastianWendorf

Description

@SebastianWendorf

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

ast-spec

Playground Link

No response

Repro Code

// ES-Lint is used for its parsing capabilities here, to parse external code and analyse it using the provided AST data

import { Node } from "@typescript-eslint/types/dist/generated/ast-spec";
import { AST_NODE_TYPES } from "@typescript-eslint/utils";

let node: Node;

// obtain AST node

if ((node.type === AST_NODE_TYPES.MethodDefinition ||
     node.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
     node.type === AST_NODE_TYPES.TSMethodSignature) &&
    !node.computed &&
    !!node.parent &&
    (node.parent.type === AST_NODE_TYPES.ClassDeclaration || node.parent.type === AST_NODE_TYPES.TSInterfaceDeclaration)) { ... }

ESLint Config

No response

tsconfig

{
    "include": ["./src/**/*"],
    "compilerOptions": {
        "target": "ESNext",
        "experimentalDecorators": true,
         "module": "Node16",
         "moduleResolution": "node16",
         "paths": {
             "@typescript-eslint/types/dist/generated/ast-spec": ["./node_modules/@typescript-eslint/types/dist/generated/ast-spec"]
         },
        "resolveJsonModule": true,
        "allowJs": true,
        "outDir": "dist",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitOverride": true,
        "skipLibCheck": true
    }
}

Expected Result

The code compiles and the check works as expected.

Actual Result

TypeScript reports the following compile error: This comparison appears to be unintentional because the types 'AST_NODE_TYPES.ClassBody | AST_NODE_TYPES.TSInterfaceBody | AST_NODE_TYPES.TSTypeLiteral' and 'AST_NODE_TYPES.ClassDeclaration' have no overlap.

When changing the comparison code the suggested types and debugging to the location in the code during execution, the runtime types match the original version. There's an obvious discrepancy between the runtime types and the types defined for TypeScript. This bug only occurred after upgrading from 7.x to 8.x.

Additional Info

The issue seems to be that @typescript-eslint/types/dist/ts-estree.d.ts seems to provide the wrong type information, e.g.:

interface MethodDefinitionNonComputedName {
parent: TSESTree.ClassBody;
}

See also here for the original source code

Versions

package version
@typescript-eslint/eslint-plugin 8.1.0
@typescript-eslint/parser 8.1.0
@typescript-eslint/typescript-estree 8.1.0
TypeScript 5.5.4
ESLint 9.9.0
node 22.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfix: user errorissue was fixed by correcting the configuration / correcting the codelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.working as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions