Skip to content

Types declaration on "Static" members of a function are invalid #31882

Description

Typescript generates invalid declaration when you declare a "static" element on a function and the name of this "static" element is exactly the same on the other side of the equality. Hard to explain but a small example below demonstrate the issue.

Note, this issue only happens when you add a function or a class as a "static" element of a function.

TypeScript Version: 3.6.0-dev.20190612

Search Terms:

'X' is referenced directly or indirectly in its own type annotation.

Code

import {Foo} from './foo';
export default function Example() {
    console.log("Example 2");
}
Example.Foo = Foo

Temporary Solution

import {Foo as DifferentNameFoo} from './foo';
export default function Example() {
    console.log("Example 2");
}
Example.Foo = DifferentNameFoo

Expected behavior:

import { Foo as GenericFoo } from './foo';
declare function Example(): void;
declare namespace Example {
    var Foo: typeof GenericFoo;
}
export default Example;

The expected behaviour is tricky because if you export in the same file Foo you have another issue.

A solution could be:

import { Foo as GenericFoo } from './foo';
export { GenericFoo as Foo };
declare function Example(): void;
declare namespace Example {
    var Foo: typeof GenericFoo;
}
export default Example;

Again another issue if Foo is a function in the same file.

Actual behaviour:

import { Foo } from './foo';
declare function Example(): void;
declare namespace Example {
    var Foo: typeof Foo;
}
export default Example;

Playground Link:
Go see this repo for more examples.
https://github.com/alexandcote/typescript-example

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions