Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mheiber opened this issue Feb 23, 2020 · 4 comments · Fixed by #37050
Closed

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

mheiber opened this issue Feb 23, 2020 · 4 comments · Fixed by #37050
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@mheiber
Copy link
Contributor

mheiber commented Feb 23, 2020

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!

@mheiber mheiber changed the title ES private field emit is incorrect ES private field emit of #private in declaration files leads to language service warnings Feb 23, 2020
@mheiber mheiber changed the title ES private field emit of #private in declaration files leads to language service warnings language service warns about ES private field emit in declaration files (#private) Feb 23, 2020
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 25, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.3 milestone Feb 25, 2020
@sandersn
Copy link
Member

@mheiber I think in your first example the emit should not include unknown, right?

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

That's what I get when trying to repro this, at least.

@sandersn
Copy link
Member

The implicit any error was fixed in #36640. I note that the language service puts this warning on lots of different ambient declarations:

export declare class AA {
    #private; // warning
    private y; // warning
    x: number; // ok
    constructor();
}
declare class BB { // warning
    x: number // ok
}

I don't think this is worth a patch fix since it's just a language service warning, and one people have been living with on private since it was introduced, I assume. It's also not clear whether the BB warning should be removed either, so there might be a little design thought needed to fix this bug.

(Also there is an almost-done version of the playground that does have dts output: https://www.typescriptlang.org/v2/en/play. I'm not sure if the url reflects which output tab is selected. However, the playground doesn't request or show suggestion diagnostics, so it's still not usable to repro this.)

@sandersn sandersn added the Fix Available A PR has been opened for this issue label Feb 26, 2020
@mheiber
Copy link
Contributor Author

mheiber commented Feb 26, 2020

@

@mheiber I think in your first example the emit should not include unknown, right?

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

That's what I get when trying to repro this, at least.

That's right, I pasted wrong. Thanks for checking.

@mheiber
Copy link
Contributor Author

mheiber commented Feb 28, 2020

Thanks @sandersn !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants