Skip to content

Rule enforcing typeAnnotation on a complex query is correct #340

Open
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Copying conversation from typescript-eslint/typescript-eslint#4065 (comment) & typescript-eslint/typescript-eslint#6444 (comment): typescript-eslint doesn't have a way to infer the types of nodes when the ESQuery selector is complex:

return {
  'ClassDeclaration, ClassExpression[id]'(
    node
    // ^? TSESTree.Node
    // Expected: something like...
    // TSESTree.ClassDeclaration | TSESTree.ClassExpression & { id: TSESTree.Identifier }
  ) {

Even with these two not(-yet?)-implemented helpers (neither of which may ever be useful for public consumers):

...there's no way for custom rules written in TypeScript to enforce that the type of node matches its AST shape. Which means devs can write blatantly wrong code like:

return {
  'ClassDeclaration, ClassExpression[id]'(
    node: TSESTree.Identifier

Is eslint-plugin-eslint-plugin an appropriate place to write a lint rule that makes sure the type annotation on a node matches the node itself?

This rule might be tricky to write given that microsoft/TypeScript#9879 blocks having a TypeScript API to check type assignability. The rule could use basic string comparisons in the meantime.

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