-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Proposal private-fields-in-in is stage-3 #42574
Comments
Good news! A few of us at @bloomberg have started discussing working on this as a TS contribution |
Work has now started on this and is going well (basic parsing with simple errors and type checking).
Type Narrowingclass MyClass {
#branded = true;
prop = Math.random();
equals(x: unknown) {
if (typeof x === 'object' && #branded in x) {
return x.prop === this.prop;
// ^ - should we narrow 'x' to 'MyClass' here?
}
return false;
}
} Pragmatically However this check can also be made to pass in slightly more esoteric code where class SuperReturn {
constructor(x: any) { return x; }
}
class MyClass extends SuperReturn {
#branded = true;
constructor(x: unknown) {
super(x);
}
static getBranded(x: MyClass) {
return x.#branded;
}
}
const a = {};
new MyClass(a);
// 'a' will now have the #branded private field
assertTrue(MyClass.getBranded(a as any));
// but is not an instance of the class
assertFalse(a instanceOf MyClass); Do we want the type narrow to go for the (assumed) common case or for the correct case? Or maybe there is a middle ground e.g. only narrow when the classes does not extend another class (I believe this is the only way the check can be 'corrupted') Looking at |
This feature has been marked by TC39 as finished. I have such questions.
Best regards. |
ES2021 was finalised before this proposal went to stage 4 so it will be part of ES2022 |
Suggestion
https://github.com/tc39/proposal-private-fields-in-in is stage 3 now.
🔍 Search Terms
esnext. private field. class
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
📃 Motivating Example
💻 Use Cases
The text was updated successfully, but these errors were encountered: