benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ASTPath<Function> is not assignable to parameter of type ASTNode

danielo515 opened this issue · comments

Hello, thanks for this great tool and its types.
Just found a situation that I don't understand fully. Seems that ASTPath is not compatible with ASTPath, which is weird because Function just extends ASTNode (as far as I know)

The whole error is like this:

  Overload 2 of 2, '(source: ASTNode | ASTNode[] | ASTPath<ASTNode> | ASTPath<ASTNode>[]): Collection<any>', gave the following error.
    Argument of type 'ASTPath<Function>' is not assignable to parameter of type 'ASTNode | ASTNode[] | ASTPath<ASTNode> | ASTPath<ASTNode>[]'.
      Type 'NodePath<Function, Function>' is not assignable to type 'ASTPath<ASTNode>'.
        Type 'Function' is not assignable to type 'ASTNode'.
          Property 'key' is missing in type 'Function' but required in type 'ClassPrivateMethod'.ts(2769)

The error is happening when I have a Function node that then I try to pass to JSCodeshift, which function signature is as you see above. If I just ignore the type error the code seems to work fine, but I am not sure if it is because I am only getting "compatible" nodes because Function is compatible with many subtypes.

Some example code may look like this:

j(file.source).find(j.Function).filter((path) => {
  j(path) //  <- here is where the error happens
})