Skip to content

Intermediate Variable assigment break inference #24376

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

Closed
rulonder opened this issue May 24, 2018 · 2 comments
Closed

Intermediate Variable assigment break inference #24376

rulonder opened this issue May 24, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rulonder
Copy link

TypeScript Version: 2.8.3

Search Terms:
Variable Union
Code

// A *self-contained* demonstration of the problem follows...
enum Types {
    A = "A",
    B = "B",
}

type ActionA = {type:Types.A,id:number}
type ActionB = {type:Types.B,code:number}

type Actions = ActionA | ActionB

function returnActionCode(action:Actions){
    switch (action.type) {
        case Types.A:
            return action.id   
        default:
            action.code;
    }
}

// assigning a intermediate variable breaks the flow analysis
function returnActionCodeFail(action:Actions){
    const type = action.type
    switch (type) {
        case Types.A:
            return action.id   // Property 'id' does not exist on type 'Actions'. Property 'id' does not exist on type 'ActionB'.
        default:
            action.code; // Property 'code' does not exist on type 'Actions'. Property 'code' does not exist on type 'ActionA'.
    }
}

Expected behavior:

Both functions should compile as they are equivalent

Actual behavior:
Only the first one is valid, Both are actually equal and should be valid, seems that having an intermediate variable assignment breaks the type inference.
Playground Link:
Playgorund Link
Related Issues:
22093

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 24, 2018
@RyanCavanaugh
Copy link
Member

Logged before / basically the same as #22341 - we don't do narrowing based on indirect references to discriminants

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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