• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

I am using JSCodeshift to migrate some old JS files. One of the things I have to do is find all the functions (both normal and arrow functions) and then add some code to their body. To find both I just use the general Function constructor to search for all the possible functions. However, when I try to use the find nodes on the jscodeshift function I get a type error. So, this simple code can work as an example:

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

This is the full error:

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)

Looking at the definition of Error, it is extending ASTNode, so it should be compatible, no?

//ast-types/gen/namedTypes.d.ts
    interface Function extends Node {
        id?: K.IdentifierKind | null;
        params: K.PatternKind[];
        body: K.BlockStatementKind;
        generator?: boolean;
        async?: boolean;
        expression?: boolean;
        defaults?: (K.ExpressionKind | null)[];
        rest?: K.IdentifierKind | null;
        returnType?: K.TypeAnnotationKind | K.TSTypeAnnotationKind | null;
        typeParameters?: K.TypeParameterDeclarationKind | K.TSTypeParameterDeclarationKind | null;
        predicate?: K.FlowPredicateKind | null;
    }

However if I use ArrowFunctionExpression as find query:

j(file.source).find(j.ArrowFunctionExpression).filter((path) => {
  j(path)
})

it doesn't complain about the type, which is weird given that ArrowFunctionExpression just extends Function type

    interface ArrowFunctionExpression extends Omit<Function, "type" | "id" | "body" | "generator">, Omit<Expression, "type"> {
        type: "ArrowFunctionExpression";
        id?: null;
        body: K.BlockStatementKind | K.ExpressionKind;
        generator?: false;
    }
about 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error