-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Exhaustiveness checking with classes #20512
Comments
Here readonly kind: "none"; You need to write this readonly kind: "none" = "none"; The former only declares that there is a The minimal example to play with is class A {
kind: "a" /* = "a" */;
}
var x = new A();
console.log(x.kind); // undefined, not "a" |
This is a runtime error, not a compile-time one. The problem is that the export class Some<A> {
readonly kind: "some";
readonly value: A;
constructor(value: A) {
this.value = value;
this.kind = "some"; // fix in the constructor
}
...
}
export class None<A> {
readonly kind: "none" = "none"; // .. or inline
...
} |
Thank you @RyanCavanaugh and @gcnew. Really appreciate you taking the time to answer even though this turned out to be a question (not a bug or suggestion). I'm up and running now.
Amazing that you're also guarding against developer incompetence in the compiler - thanks again! |
I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged unions or algebraic data types that involve classes. I am coming from Scala which supports it, but each language is different and I guess you may be limited by Javascript. Here's some code:
TypeScript Version: 2.6.0
Code
Maybe.ts
Maybe.spec.ts
Expected behavior:
Test passes
Actual behavior:
Test fails
Work around code
Maybe.ts
Maybe.spec.ts
Thanks for the fun, manageable language.
The text was updated successfully, but these errors were encountered: