Skip to content

[api-extractor] import() syntax generates invalid dts rollup result sometimes #5326

@adventure-yunfei

Description

@adventure-yunfei

Summary

when using together with manual import, import() syntax may generate invalid dts rollup result.

Repro steps

run dts rollup with following dts input file:

import { FooModule } from 'foo';
export interface Foo {
    prop: FooModule.FooClass;
    prop_importType: import('foo').FooModule.FooClass;
}
export {}

Expected result:
Generate dts rollup result:

import { FooModule } from 'foo';
export declare interface Foo {
  prop: FooModule.FooClass;
  prop_importType: FooModule.FooClass;
}
export { }

Actual result:

import { FooModule } from 'foo';
export declare interface Foo {
  prop: FooModule.FooClass;
  prop_importType: FooModule;  // this should be FooModule.FooClass
}
export { }

Details

Found the reason after debug. When fetchAstImport, the result is cached by key from AstImport.getKey. But AstImport with AstImportKind.ImportType type generates the same key with other type. So when both types exist in the dts file, they are all resolved to the same AstImport instance. And the import type mismatching will cause error. e.g. when emitting dts rollup, only AstImoprt with ImportType is handled for import() syntax (in modifyImportTypeSpan).

Maybe we should ensure that the same cache key always refers to the same result.

Standard questions

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

Question Answer
@microsoft/api-extractor version? 7.52.11
Operating system? Mac
API Extractor scenario? rollups (.d.ts)
Would you consider contributing a PR? Yes
TypeScript compiler version? 5.8.2
Node.js version (node -v)? v16.20.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions