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
If I add a new case to the Expression type (e.g., for subtraction), then the type checker kindly points out all the places I need to update to handle that case. I rely on this heavily and greatly appreciate it.
However, TypeScript reports a type error if there is only one case:
typeExpression={tag: "constant",value: number};exportfunctionunreachable(x: never): never{returnx;}functionevaluate(expression: Expression): number{switch(expression.tag){case"constant":
returnexpression.value;default:
returnunreachable(expression);// Argument of type 'Expression' is not assignable to parameter of type 'never'.}}console.log(evaluate({tag: "constant",value: 2}));
This is surprising to me, since I would expect this situation with only 1 case to behave exactly the same as with N cases in general.
One might be tempted to think that this is not useful. But I think there are situations where it makes sense to start with only one case if it is expected that there will be more added in the future (YAGNI might be a valid counterargument in many situations—but I don't believe the type checker should proclaim that).
On a related note, I would also like this to work on types with zero cases (i.e., never), which is justified because such a type is not supposed to be able to be instantiated. I think that would look like this:
typeExpression=never;// Zero cases!exportfunctionunreachable(x: never): never{returnx;}functionevaluate(expression: Expression): number{switch(expression.tag){// Property 'tag' does not exist on type 'never'.default:
returnunreachable(expression);}}
But perhaps I am asking for too much with that example!
Essentially, I want 0 variants and 1 variant to be unremarkable non-special cases of N variants and behave uniformly as such, as they do in languages like Rust and Haskell.
🔎 Search Terms
"exhaustive pattern matching"
"pattern matching"
"algebraic data types"
I apologize if this has been raised before.
🕗 Version & Regression Information
This is the behavior in every version I tried (3.3.3-4.5.4), and I reviewed the FAQ for entries about "Type System Behavior".
generated/types.ts:17:26 - error TS2345: Argument of type 'Expression' is not assignable to parameter of type 'never'.
17 return unreachable(expression);
~~~~~~~~~~
Found 1 error.
🙂 Expected behavior
I expect it to type check successfully and print 2 to standard output when executed.
Thank you for reading this bug report!
The text was updated successfully, but these errors were encountered:
Ah, thanks! I missed it since I only searched for open issues. So it seems like this is simply "wont fix"—is that right?
One reason I'd like this to work is because I'm currently writing a code generator (à la Protocol Buffers/Thrift), and generating valid TypeScript code despite these special cases in the type system is proving to be somewhat laborious and error-prone. But perhaps code generators are too niche of a use case to motivate a fix for this.
Bug Report
A pattern that I love in TypeScript is the idea of exhaustive pattern matching on a tagged union:
If I add a new case to the
Expression
type (e.g., for subtraction), then the type checker kindly points out all the places I need to update to handle that case. I rely on this heavily and greatly appreciate it.However, TypeScript reports a type error if there is only one case:
This is surprising to me, since I would expect this situation with only 1 case to behave exactly the same as with N cases in general.
One might be tempted to think that this is not useful. But I think there are situations where it makes sense to start with only one case if it is expected that there will be more added in the future (YAGNI might be a valid counterargument in many situations—but I don't believe the type checker should proclaim that).
On a related note, I would also like this to work on types with zero cases (i.e.,
never
), which is justified because such a type is not supposed to be able to be instantiated. I think that would look like this:But perhaps I am asking for too much with that example!
Essentially, I want 0 variants and 1 variant to be unremarkable non-special cases of N variants and behave uniformly as such, as they do in languages like Rust and Haskell.
🔎 Search Terms
I apologize if this has been raised before.
🕗 Version & Regression Information
This is the behavior in every version I tried (3.3.3-4.5.4), and I reviewed the FAQ for entries about "Type System Behavior".
⏯ Playground Link
💻 Code
(This is the problematic example from above.)
🙁 Actual behavior
TypeScript reports this type error:
🙂 Expected behavior
I expect it to type check successfully and print
2
to standard output when executed.Thank you for reading this bug report!
The text was updated successfully, but these errors were encountered: