question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support open-ended unions

See original GitHub issue

Suggestion

Ability to discriminate between union members when not all union members are known.

Use Cases

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:open
  • Created 5 years ago
  • Reactions:7
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
DanielRosenwassercommented, Feb 17, 2021

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

  • Using keyof { well: any, known: any, fields: any, [x: string]: any } where tooling picks up on well-known properties from keyof for completions, but the effective type is just string. (ugh)
  • string | SomeEnumType (less ugh), or just string with some documented enum to users can pass through.
2reactions
RyanCavanaughcommented, Aug 13, 2018

Open questions here:

  • What (if anything) prevents the declaration of two things with the same kind ?
  • How does this work in a “pull” typechecking model? To know what to do in the switch, we’d have to exhaustively check the entire program to find all possible declarations
  • Where do the perf wins come from? Isn’t the type of s in the default block (if we imagine hundreds of other shapes) still an enormous union? Or does an open-ended union imply the existence of subtraction types?
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found