Skip to content

[api-extractor] Runtime error during model generation for readonly class properties (regression)  #3423

@maxakuru

Description

@maxakuru

Repro steps

Running API Extractor on a class with readonly properties with no type annotation but with an inline initialization crashes with error:

ERROR: Cannot read properties of undefined (reading '0')

Repro repo: https://github.com/maxakuru/api-extractor-bug

Expected result:

Readonly class properties without an initializer should ideally have their type inferred from the default value.

Actual result:

Crashes due to reading property from undefined

Details

Looks to be caused by this recent change and maybe the comment above about it being a setter isn't necessarily true.

Not sure if the ideal fix is here or in TS, but I'm guessing the output difference from tsc for readonly props with/out type annotations is intentional.

Example with annotation:

// .ts
export class Test {
  readonly foo: string = 'ok';
}

// .d.ts
export declare class Test {
  readonly foo: string = 'ok';
}

Example without annotation (fails in api-extractor):

// .ts
export class Test {
    readonly foo = "bar";
}

// .d.ts
export declare class Test {
    readonly foo = "bar"; // should this be readonly foo: "bar" ?
}

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@microsoft/api-extractor version? 7.24.0
Operating system? mac
API Extractor scenario? rollups/model generator
Would you consider contributing a PR?
TypeScript compiler version? 4.6.4
Node.js version (node -v)? 16.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions