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
When a hashmap object property is accessed by a union discriminant, check if the resulting property covaries with the discriminant in a compatible way.
Currently: the line with <--- ! comment would incur a type error, since union cannot fit into the intersection of UnionComponentA & UnionComponentB & UnionComponentC, which is the safe way of making sure that union can be accepted by any property of router.
Wanted: in reality, we could infer that the type of union is discriminated in way that it will always match the call signature that is the result of indexing route. This inference (I guess) would require that the indexed route function property is invoked immediately with the discriminated union as argument.
Use Cases
This can, in some cases, be much more concise than a long switch. This is especially true if handle* functions above accept multiple arguments, and only one is polymorphic that would currently require use of switch to resolve:
switch(union.discriminant){case"A":
handleA(some,long,arglist,before,the,discriminated,union)// the last position is polymorphicbreakcase"B":
// ... same deal ...}
Disclaimer: I don't know if this kind of reasoning is too "niche", and maybe somebody more knowledgeable than me with TypeScript's internal can generalize what I'm asking for here or present an existing pattern that fulfill the spirit of what this suggestion would achieve. Another case that would employ a similar kind of inference might be something like assigning a discriminated union to a specific, compatible property of route; but I don't see a practical use case of this.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code <-- (Um, I believe it doesn't, but more knowledgeable people can maybe correct me!)
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
This feature would agree with the rest of TypeScript's Design Goals. <--- I think it does..., again would welcome any explanation otherwise!
The text was updated successfully, but these errors were encountered:
Search Terms
discriminated union, hashmap, switch, object, map, router
Suggestion
When a hashmap object property is accessed by a union discriminant, check if the resulting property covaries with the discriminant in a compatible way.
Example
Currently: the line with
<--- !
comment would incur a type error, sinceunion
cannot fit into the intersection ofUnionComponentA & UnionComponentB & UnionComponentC
, which is the safe way of making sure thatunion
can be accepted by any property ofrouter
.Wanted: in reality, we could infer that the type of
union
is discriminated in way that it will always match the call signature that is the result of indexingroute
. This inference (I guess) would require that the indexedroute
function property is invoked immediately with the discriminated union as argument.Use Cases
This can, in some cases, be much more concise than a long switch. This is especially true if
handle*
functions above accept multiple arguments, and only one is polymorphic that would currently require use ofswitch
to resolve:Disclaimer: I don't know if this kind of reasoning is too "niche", and maybe somebody more knowledgeable than me with TypeScript's internal can generalize what I'm asking for here or present an existing pattern that fulfill the spirit of what this suggestion would achieve. Another case that would employ a similar kind of inference might be something like assigning a discriminated union to a specific, compatible property of
route
; but I don't see a practical use case of this.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: