Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda