You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Classes with private #fields are not treated as distinct when returned from functions #62664
"class from functions", "classes returned from functions", "class type" functions, "ES2020", "private fields", "#private"
🕗 Version & Regression Information
I believe this behavior has not changed since the introduction of private #fields in classes. I reviewed the FAQ about anything relating to classes with #fields.
classA{ #a: string;constructor(a: string){this.#a =a}}classB{ #a: string;constructor(a: string){this.#a =a}}leta: A=newA("Hello");letb: B=newA("Hello");// Error: Type 'A' is not assignable to type 'B'. Property '#a' in type 'A' refers to a different member that cannot be accessed from within type 'B'.// Error is the desired behaviour herefunctiontest(){returnclassNamed{ #a: string;constructor(a: string){this.#a =a;}}}letC=test();letD=test();letc=newC("1");letd: typeofc=newD("1");// No error
🙁 Actual behavior
a value of type D can be assigned to a value of type C without error.
🙂 Expected behavior
C and D are treated as distinct types like A and B, so the same error happens, ie. that the property #a of type D refers to a different member that cannot be accessed from within type C (typeof c).
Additional information about the issue
This would enable the ability to create new distinct class types dynamically which would be great for when you want to make unique types that cannot be satisfied by any other value.
Activity
MartinJohns commentedon Oct 23, 2025
Duplicate of #56146.
RyanCavanaugh commentedon Oct 23, 2025
MartinJohns commentedon Oct 23, 2025
There's an open PR #60885 by Mateusz Burzyński (@Andarist) that would make use of #fields in class expressions an error.