-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed as duplicate of#56146
Closed as duplicate of#56146
Copy link
Description
🔎 Search Terms
"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.
⏯ Playground Link
💻 Code
class A { #a: string; constructor(a: string) { this.#a = a } }
class B { #a: string; constructor(a: string) { this.#a = a } }
let a: A = new A("Hello");
let b: B = new A("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 here
function test() {
return class Named { #a: string; constructor(a: string) { this.#a = a; } }
}
let C = test();
let D = test();
let c = new C("1");
let d: typeof c = new D("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.
Metadata
Metadata
Assignees
Labels
No labels