Skip to content
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

Private symbol class properties treated as public #20080

Closed
dpogue opened this issue Nov 16, 2017 · 8 comments
Closed

Private symbol class properties treated as public #20080

dpogue opened this issue Nov 16, 2017 · 8 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fixed A PR has been merged for this issue

Comments

@dpogue
Copy link

dpogue commented Nov 16, 2017

TypeScript Version: 2.7.0-dev.20171116

Code

const _data = Symbol('data');

export class User {
    private [_data] : any;
};

Expected behavior:
Compiles successfully.

Actual behavior:
TS4031: Public property '[_data]' of exported class has or is using private name '_data'.

The issue is that [_data] is not a public property, so it shouldn't care if it's using a private name because it's not meant to be visible outside of the User class.

@mhegazy mhegazy added Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files labels Nov 17, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 17, 2017

some background in #19545 (comment).

Today there is no way to write this file a .d.ts and not expose _data. that is basically what the error is telling you.

we can possibly fix this by adding an auto-generated export {} to the bottom of the generated .d.ts and not show the error.

@dpogue
Copy link
Author

dpogue commented Nov 17, 2017

I think I was under the assumption that private properties don't get emitted to the .d.ts files. Is that not the case?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 17, 2017

I think I was under the assumption that private properties don't get emitted to the .d.ts files. Is that not the case?

They need to be emitted to the .d.ts to avoid derived classes creating members with he same name.

@DanielRosenwasser
Copy link
Member

Two things:

  1. The symbol is technically still accessible, it's just not possible to reference it directly.
  2. Automatically exporting the symbol seems questionable.

@dpogue
Copy link
Author

dpogue commented Nov 17, 2017

They need to be emitted to the .d.ts to avoid derived classes creating members with he same name.

If the symbol isn't exported, it's literally unknowable, so you can't have the same name it in a derived class (which is, of course, our entire reason for not exporting it)

@mhegazy
Copy link
Contributor

mhegazy commented Nov 18, 2017

If the symbol isn't exported, it's literally unknowable, so you can't have the same name it in a derived class (which is, of course, our entire reason for not exporting it)

There is some historical context why things work the way they do.. please see #19545 (comment) for more details.

@weswigham
Copy link
Member

weswigham commented Nov 20, 2017

@dpogue I've opened a PR to elide the property like @mhegazy was talking about; but your original error in the OP is not the error you'd get from the declaration emit of that property - from my explorations, the error you've given occurs when a global Symbol type can't be found - do you have lib: ["es6"] included?

@dpogue
Copy link
Author

dpogue commented Nov 20, 2017

I do have lib: [..., "es2015.symbol", ...] in my tsconfig.json

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants