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

Type inference in switches over expressions pointing to nested properties does not work #41830

Closed
u840903 opened this issue Dec 5, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@u840903
Copy link

u840903 commented Dec 5, 2020

TypeScript Version: 4.1.2, 4.2.0-dev.20201205

Search Terms: type inference switch statement

Code

enum TYPES {
    ONE = 'one',
    TWO = 'two'
}

interface IOne {
    a: string,
     type: TYPES.ONE
     meta: {
         type: TYPES.ONE
     }
}

interface ITwo {
    b: string,
    type: TYPES.TWO
    meta : {
        type: TYPES.TWO
    }
}

[].map((block:IOne | ITwo) => {
    // This works.
    switch (block.type) {
        case TYPES.ONE:
            block.a 
            break;
        case TYPES.TWO:
            block.b 
            break;
    }

    // This does NOT work.
    switch (block.meta.type) {
        case TYPES.ONE:
            block.a 
            break;
        case TYPES.TWO:
            block.b 
            break;
    }
});

Expected behavior:
The switch over block.meta.type should infer the same types as the switch over block.type.

Actual behavior:
The swich over block.meta.type errors the following and does not infer type correctly. The switch over block.type DOES infer type correctly.

Property 'a' does not exist on type 'IOne | ITwo'. Property 'a' does not exist on type 'ITwo'.
Property 'b' does not exist on type 'IOne | ITwo'. Property 'b' does not exist on type 'IOne'.

Playground Link:
https://www.typescriptlang.org/play?ts=4.1.2#code/KYOwrgtgBAKgmgBQKIGUoG8BQUdQPIBySUAvFAOQD2Iw5ANNrjAOp6kUAuA7peZgL6ZMASxAdgAJwBmAQwDGwKAEk8NDIxwyAXFADOHCaIDmDXDg4BPAA7Ad8ZCgB0hJBpwRgHberO-LNu0RUZyI3KEFBETFJWQVlGB4fXAAjHX1DEBM3f1tYIKcWPDcPLygdLF9cHMCHR0K3CKEAbQBdRwgZKwAKLuSAG0o5AGstFTUAH3ieAEpSAD4knAB6JdgAC2FdKB4JId1HN10uYQ45NahegeHHHNmKyqg5GV1Fe2CXLTCH-sGhxxkoF9KskJMAZEMANxAx7PV75OqsT4PXw-a7JQHIswgsGQhpCMwrdabKAAE0owC2BDwMG2lF2BzMRxOZwuqL+JRkN2swDu0KeLzytQ+0KxVz+AJFKVB4KhyP5cNqhSRmJwbMc6MlquluLMEWmEKAA

Related Issues:
#30763
#30557
#30593

Note:
I'm sorry if this is obvious or at least an obvious duplicate. I've searched, I've asked on Gitter and Stack Overflow. I'm fairly new to Typescript and this is my first bug repport. I did my best.

@RyanCavanaugh
Copy link
Member

Duplicate #18758

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 7, 2020
@u840903
Copy link
Author

u840903 commented Dec 7, 2020

Possible workaround using a combination of type guards and generics can be found here. https://stackoverflow.com/questions/65139900/when-does-typescript-type-inference-in-switch-statements-work

@u840903 u840903 closed this as completed Dec 7, 2020
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

2 participants