typescript-compiler-api

Why isClassDeclaration finds interface declarations instead in typescript compiler api?


I can't understand why when I check the node with condition like

    if (node.kind === ts.SyntaxKind.ClassDeclaration) {...}

I'm getting interface declaration instead? But class declaration has kind 262 (FunctionDeclaration)

    @Visit(ts.SyntaxKind.ClassDeclaration)
    public classDeclarationVisitor(node: ts.ClassDeclaration) {
        console.log("class declaration", node.getText());
        const hasName = node.name && ts.isIdentifier(node.name);
        const className = hasName ? node.getText() : "";
        const implementedInterfaces = getImplementedInterfaces(node);
        return node;

    }

you can see output on screenshot output

Can you please help me, how I can get class declarations using this condition, maybe I have wrong configurations? I'm trying to create a DI container that can work with interfaces and inject instances of classes that implemented some interfaces by type in constructor, I've already figured out the main logic, but faced this trouble.

I tried to check other transformer libraries, but I can't understand the difference. I also tried to use isClassDeclaration type guard function but it works the same


Solution

  • My guess is that the code is somehow mixing two different versions of TypeScript where the definition of ts.SyntaxKind has different member values (they really should pin all SyntaxKind members to a specific value, but they don't). Ensure you're only using one version of TypeScript.