Skip to content
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

Switch statements should discriminate by class prototype #38373

Closed
karol-majewski opened this issue May 6, 2020 · 4 comments
Closed

Switch statements should discriminate by class prototype #38373

karol-majewski opened this issue May 6, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@karol-majewski
Copy link

TypeScript Version: Nightly (4.0.0-dev.20200505)

Search Terms:

  • switch
  • control flow
  • discriminated union
  • untagged union
  • disjoin
  • prototype
  • constructor
  • 2339

Code

class Failure {}

class Success {
    constructor(readonly value: string) { }
}

function main(result: Failure | Success): void {
    switch (result.constructor) {
        case Success:
            console.log(`Success! Your value is ${result.value}`); // Property 'value' does not exist on type 'Failure | Success'.
            break;
        case Failure:
            console.log('Oh noes');
    }
}

main(new Success("🍆"));
Output
"use strict";
class Failure {
}
class Success {
    constructor(value) {
        this.value = value;
    }
}
function main(result) {
    switch (result.constructor) {
        case Success:
            console.log(`Success! Your value is ${result.value}`); // Property 'value' does not exist on type 'Failure | Success'.
            break;
        case Failure:
            console.log('Oh noes');
    }
}
main(new Success("🍆"));
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Expected behavior:

No compile-time error. result.value should be of type string. That's what is happening in runtime.

Actual behavior:

We get a type error.

Property 'value' does not exist on type 'Failure | Success'. (2339)

Note that it works if you use if statements.

class Failure {}

class Success {
    constructor(readonly value: string) { }
}

function main(result: Failure | Success): void {
    if (result instanceof Success) {
        console.log(`Success! Your value is ${result.value}`);
    }

    if (result instanceof Failure) {
        console.log('Oh noes');
    }
}
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 8, 2020
@RyanCavanaugh
Copy link
Member

This code doesn't work in the presence of e.g. class GreatSuccess extends Success { ... } - is this an intentional gap?

@karol-majewski
Copy link
Author

karol-majewski commented May 10, 2020

It's not — I haven't considered subclass relationships.

A bit of context. I'm using classes to model states in my program. They are never inherited from. My program yields well-defined results such a Success<T>, Failure, or a Timeout. These types form a union. I was looking for a neat way to disjoint that union. A switch statement seemed appropriate.

@RyanCavanaugh
Copy link
Member

Duplicate #16035

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Aug 28, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants