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

Nested union variant not infered correctly #59455

Closed
jpqx opened this issue Jul 29, 2024 · 3 comments
Closed

Nested union variant not infered correctly #59455

jpqx opened this issue Jul 29, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@jpqx
Copy link

jpqx commented Jul 29, 2024

🔎 Search Terms

"union type inference", "union type variant inference"

🕗 Version & Regression Information

This is the behavior in every version I tried (all the way back to 3.3.3333 in playground), and I reviewed the FAQ for entries about "common-bugs-that-arent-bugs".

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAMg9gYwIYBsCCBXYALAysJYaAXgCgoKoAfKAbyiS2wgDtgBLZIgEwC4oAZqgDOEADRQA7knYcWAcwBicAE4AlCAFs4RTDlYcuEPlABGcOCghIWUAL7lKNeo31tOhY-2AqM4qCxImhD8wj7sCvakpKCQUBraukz4nlBklNR0DEwGHjz8QiiiURnO2W6GniY+fhKBwaHhkQ4x4NAAsjIsengERGmOFGWuzO5GJoXFDqVZI7nj3r7QdlAAZFAAFIMZmfQoiKj8AKIAHj5ICMAAPPDI6Ml9-i45Y1WLfvYAfBIqWjoh8T+SRwKX60x2Qyy+zu-FuqB6oP8v0SANO50uVwS-wRjwkzwqeS8UBqy0+JUoAEpogB6alQNAsHTMFRQbgQAQRWTsOC2ASqKCdCI41IbYRBaCxfwAKwwYSg2l+UAAbhAVOZinyVJpCBJJLJsHAsFBZRF5FAAAZolQXYDmikSUxGsXBKCqlSqAB0pGpACpWnFBd0Hql0k5Zi9KvlBCJltthhHCdUlvY1pttjM9gcUPx8aNI0SSXVxY0VKb7D8gQDc-M3sSlkWGlAwqXmuSIWVoYdwwSFnXagFi02mma7BWUTnynnEwUYynwe2oVmJ3NXlHJv5pFyFMp1JWejWo+ZLNZbKOoMj-suE73CwPG82yyt51AqT7qdEBBgWJdubZhCBv2FIhhA2bUhWDIh+EDICIApKCuhguhtnYARNjAoMQUeD0V3zbg1nWABCdCYI9TsUGw68qnw+UEIgiBSKzD1NzkJRVCxYEp3GCkkIhChfmADAVFsWh014j1xOIuixFEiEyKvHta1veoAUkzDPA9C8iA9ZSU1pKAAAlVWgABJKB5AgYAoAQOBNDAdgrFdFR3RZc0AAV3UgFRQCgAByZSfNZOAIGEAIdFdE52DlHliTaXz2IgGCfK9Xj7AAbm2FoMj0tAijgCQULMJlJwPYwJBEOBXRYYRBOgZSQu1YAEGwaIMn4wTbFU3pPHShwgA

💻 Code

type LocalAuthState =
    | { authenticated: false, waitingForRemoteAuthenticated: boolean }
    | { authenticated: true, name: string }

type RemoteAuthState =
    | { authenticated: false }
    | { authenticated: true, name: string }

type MainAuthState =
    | { authenticated: false }
    | { authenticated: true } & (
        | { local: Extract<LocalAuthState, { authenticated: true }>, remote: RemoteAuthState }
        | { local: LocalAuthState, remote: Extract<RemoteAuthState, { authenticated: true }> }
    )

// Another definition for MainAuthState (same type, just more verbose format, without using `Extract`), but same error.
/*
type MainAuthState =
    | { authenticated: false }
    | { authenticated: true } & (
        | { local: { authenticated: true, name: string }, remote: { authenticated: true, name: string } }
        | { local: { authenticated: true, name: string }, remote: { authenticated: false } }
        | { local: { authenticated: false, waitingForRemoteAuthenticated: boolean }, remote: { authenticated: true, name: string } }
    )
*/

function syncAuthStates(mainAuthState: MainAuthState): MainAuthState {
    if (mainAuthState.authenticated && !mainAuthState.local.authenticated && mainAuthState.local.waitingForRemoteAuthenticated) {
        return {
            ...mainAuthState,
            local: { authenticated: true, name: mainAuthState.remote.name } // Here I get compile error `Property 'name' does not exist on type 'RemoteAuthState'.
        };
    }

    // Also, if both authenticated, ensure names match

    return mainAuthState;
}

🙁 Actual behavior

I get compile error: Property 'name' does not exist on type 'RemoteAuthState'..

🙂 Expected behavior

If the condition mainAuthState.authenticated && !mainAuthState.local.authenticated is met, TS should infer that mainAuthState.remote is of type { authenticated: true, name: string }.

@jpqx jpqx changed the title Union variant not infered correctly Nested union variant not infered correctly Jul 29, 2024
@jpqx
Copy link
Author

jpqx commented Jul 29, 2024

I updated the title to "Nested ...". After posting, I thought maybe it has to do with checking for nested fields of the union. Then I found this issue from 2017, I think it's the same topic. Shall we mark this one as a duplicate of that?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 30, 2024
@RyanCavanaugh
Copy link
Member

Yeah, narrowing currently can't affect "sibling" properties unless it's a literal discriminant

@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
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