Skip to content

language service warns about ES private field emit in declaration files (#private) #36963

Closed
@mheiber

Description

@mheiber

TypeScript Version: 3.9.0-dev.20200222

Search Terms:

private fields, es private fields, declaration files

Code

export class A {
    #foo = 3;
    constructor() {
        console.log(this.#foo);
    }
}

Expected behavior:

npx typescript@next 1.ts --target esnext --declaration should produce a declaration file that the language service doesn't complain about.

Actual behavior:

export declare class A {
    #private; // two language service warnings here
    constructor();
}

The language service complains about the following issues in the declaration file:

Member '#private' implicitly has 'any' type, but a better type may be inferred from usage. ts(7045)
'#private' is declared but its value is never read. ts(6133)'

Playground Link:

Can't do, since the playground doesn't yet support dts generation

Suggested solution

To get rid of the warning about implicit any, we could change the emit to:

export declare class A {
    #private: unknown;
    constructor();
}

To get rid of the warning about an unused variable, we could update the language service to never warn on unused private fields in declarations. I think this rule would ideally be applied for declarations anywhere (not just in declaration files), since the use cases for forcing nominality are probably the same in .ts files.

Happy to implement!

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions