Support open-ended unions
See original GitHub issueSuggestion
Ability to discriminate between union members when not all union members are known.
Use Cases
- redux actions: See https://github.com/Microsoft/TypeScript/issues/2214#issuecomment-352103680
- This would enable us to discriminate on Node kinds without converting Node to a huge union (which slows down compilation too much). (Also, Node is effectively open-ended because we add new kinds fairly often.)
Examples
interface Shape {
unique kind: string;
}
interface Square extends Shape {
kind: "square";
size: number;
}
interface Circle extends Shape {
kind: "circle";
radius: number;
}
// other shapes may exist
function area(s: Shape) {
switch (s.kind) {
case "square":
return s.size * s.size;
case "circle":
return Math.PI * s.radius ** 2;
default:
return 0; // Or hand off to some other function that handles other Shape kinds
}
}
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript / JavaScript code
- 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. new expression-level syntax)
Workarounds
Cast the general type to a union of known type.
function area(sIn: Shape) {
const s = sIn as Square | Circle;
...
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:12 (9 by maintainers)
Top Results From Across the Web
Employer/Union Rights and Obligations
Threats to employees that they will lose their jobs unless they support the union. Seeking the suspension, discharge or other punishment of an...
Read more >Support for labor unions in the U.S. is at a 57-year high
Support for labor unions in the U.S. is at a 57-year high, according to a new Gallup poll. Seventy-one percent of Americans now...
Read more >Unions 101
A labor union is a group of two or more employees who join together to advance common interests such as wages, benefits, schedules...
Read more >Turning the Tables: Participation and Power in Negotiations
We sought unions that practice open negotiations, by which we mean open to all workers covered by the collective-bargaining agreement.
Read more >It Starts With Talking to Each Other
You can learn the most about another person by asking open-ended questions. These are questions which can't be answered with one word. Favorite...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Something like this seems to come up quite a lot with string literals. We’ll often get a request to support N well-known strings, but in reality, the system allows arbitrary strings. Usually the intent there is that tooling should give auto-complete hints, but the type-checker should not error.
This seems to be the case in #42134.
Additionally, I’ve spoken to @bterlson and @jonathandturner about this a couple of times now on the Azure SDK. The work-arounds are
keyof { well: any, known: any, fields: any, [x: string]: any }
where tooling picks up on well-known properties fromkeyof
for completions, but the effective type is juststring
. (ugh)string | SomeEnumType
(less ugh), or juststring
with some documented enum to users can pass through.Open questions here:
kind
?switch
, we’d have to exhaustively check the entire program to find all possible declarationss
in thedefault
block (if we imagine hundreds of other shapes) still an enormous union? Or does an open-ended union imply the existence of subtraction types?