Skip to content

Incorrect declaration emit for allowJs JavaScript class with static members imported from other files #33777

@cspotcode

Description

@cspotcode

TypeScript Version: 3.7.0-dev.20191002

Search Terms:

Code

Emitted index.d.ts from these JS files using --declaration --allowJs has an avoidable semantic error. It looks like the compiler usually avoids this error, but in this particular case it doesn't.

// foo.js
function foo() {}
export {foo}
// index.js
import {foo} from './foo'

export {MyClass}
function MyClass() {}
MyClass.prototype = {
  method() {}
}
MyClass.foo = foo

Expected behavior:

This declaration is emitted:

// index.d.ts
export function MyClass(): void;
export class MyClass {
    method(): void;
}
export namespace MyClass {
    export { foo };
    import { foo } from "./foo";
}
export {};

Actual behavior:

This declaration is emitted:

// index.d.ts
export function MyClass(): void;
export class MyClass {
    method(): void;
}
export namespace MyClass {
    export { foo };
}
declare var foo: typeof foo;     // <-- foo circularly references self
import { foo } from "./foo";     // <-- import foo conflicts with var foo
export {};
//# sourceMappingURL=index.d.ts.map

It appears to be triggered by the combination of both function MyClass and MyClass.prototype = assignment. Removing one or the other fixes the bug, so that the import {foo} from './foo'; is nested within export namespace MyClass as in the "expected behavior" above.

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

Labels

DuplicateAn existing issue was already createdFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions