-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make it possible to use private fields in type declarations #51489
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
Comments
To solve the Private fields appearing in a structural position where they can never be legally accessed, and would have fairly confusing semantics (are they allowed to merge via intersection? how?) is a sort of "a screwdriver is a hammer if you hold it by the pointy end" solution -- it would work, but the right fix is clearly nominal types. |
I've been looking for an analogy for this sort of thing forever; this is perfect. |
I don't agree that it would be confusing, I expected it to work and got surprised by the error message. Private fields would work exactly like regular fields. As a matter of fact, they already kind of do, but you need to use But sure, makes sense to not do this since it could have been solved by another issue that has been opened for 8 years with apparently no work on it. /s |
A non-class type with a private field would only be useful as an opaque type that you could cast something else to; you would never be able to directly construct an object that’s assignable to it, because you can’t write object literals with private fields in JS. I agree it would be confusing to have first-class support for an entire class of types for which no legal runtime values exist. Even if that were allowed, it’s semantically confusing because If you’re instead proposing that object literals like |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
🔍 Search Terms
private field type declaration
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Make it possible to use private fields in type declarations, like:
Currently this gives an error saying that private fields can only be used inside classes. It is true that no code can be generated for private fields outside classes, but no code is generated when declaring a type.
📃 Motivating Example
Sometimes it is nice to add markers to a type that define how the type can be used (
PhantomData<T>
in Rust).In my case, I automatically generate endpoint definition like
My definition of
Endpoint
currently looks likeThis works, but the only thing that says that the body is fake is the ugly name. If I could instead write
no one could even possibly try to access the marker, and there would be no reason for the editor to ever show it.
This feature could also be used in libraries, where you might want opaque state objects to be passed between methods, like
The text was updated successfully, but these errors were encountered: