You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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".
typeLocalAuthState=|{authenticated: false,waitingForRemoteAuthenticated: boolean}|{authenticated: true,name: string}typeRemoteAuthState=|{authenticated: false}|{authenticated: true,name: string}typeMainAuthState=|{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 } } )*/functionsyncAuthStates(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 matchreturnmainAuthState;}
🙁 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 }.
The text was updated successfully, but these errors were encountered:
jpqx
changed the title
Union variant not infered correctly
Nested union variant not infered correctly
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?
🔎 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
🙁 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 thatmainAuthState.remote
is of type{ authenticated: true, name: string }
.The text was updated successfully, but these errors were encountered: