Skip to content

ExpressionWithTypeArguments.insertTypeArguments crashes on mixins #1615

@maxpatiiuk

Description

@maxpatiiuk

Trying to call .addTypeArguments() on a ExpressionWithTypeArguments that contains a mixin results in an exception:

/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/@ts-morph/common/dist/ts-morph-common.js:438
            throw new InvalidOperationError(typeof errorMessage === "string" ? errorMessage : errorMessage(), node);
                  ^

InvalidOperationError: A child of the kind Identifier was expected.

/Users/mak13180/site/esri/arcgis-web-components-3/packages/support-packages/jsapi-extractor/test.ts:1:17
> 1 | class A extends B(C) {}
    at Object.throwIfNullOrUndefined (/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/@ts-morph/common/dist/ts-morph-common.js:438:19)
    at ExpressionWithTypeArguments.getFirstChildByKindOrThrow (/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/ts-morph/dist/ts-morph.js:3857:30)
    at ExpressionWithTypeArguments.insertTypeArguments (/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/ts-morph/dist/ts-morph.js:10126:41)
    at ExpressionWithTypeArguments.addTypeArguments (/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/ts-morph/dist/ts-morph.js:10115:25)
    at <anonymous> (/Users/mak13180/site/esri/arcgis-web-components-3/packages/support-packages/jsapi-extractor/src/temp.ts:11:14)
    at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Node.js v22.12.0

Version: 25.0.1

To Reproduce

import { Project, SyntaxKind } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("test.ts", "class A extends B(C) {}");
const expression = sourceFile.getFirstDescendantByKindOrThrow(SyntaxKind.ExpressionWithTypeArguments);
expression.addTypeArguments(["D"]);

Expected behavior

No exception should occur.
Source file should be modified to look as follows:

class A extends B(C)<D> {}

Workaround

Use .replaceText():

  expression.replaceWithText(`${expression.getText()}<D>`);

Extra details

Bug occurs here:

const identifier = this.getFirstChildByKindOrThrow(SyntaxKind.Identifier);

Code should be modified to handle the case that TypeArgumentedNode contains something other than identifier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions