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
interfaceIUser{id: number;createdAt: Date;name: string;age: number;isActive: boolean;}//Imagine this array is an array of strings matching the property names of IUserletproperties=[]asany[]as(keyofIUser)[];letuser: IUser={}asany;//Imagine this being fetched from a databaseletconstructedUser: IUser={}asany;for(letpropofproperties){constructedUser[prop]=user[prop];}
Expected behavior: No errors
Actual behavior:
Type 'string | number | boolean | Date' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
The text was updated successfully, but these errors were encountered:
greg-hornby-roam
changed the title
String union type to copy properties to other object not assignable to never
Union type to copy properties to other object not assignable to never
Jul 28, 2019
I guess it's a design limitation that if the target and source objects are the same interface, and are being indexed with the same key it can't infer that in that one case the types should match?
The issue is that the compiler only sees the types when checking this - it doesn’t know that prop evaluates to the same key on both sides, only that it’s type keyof IUser. That isn’t enough information to guarantee the assignment is safe, so it rejects it.
TypeScript Version: 3.5.x
Search Terms: union properties object never
Code
Expected behavior: No errors
Actual behavior:
This used to work on 3.4, seems broken on 3.5
Playground Link:
3.5: (Broken)
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIFUDO1kG8BQyRywAJgFzIgCuAtgEbQDchxCUEckpAgmJQBEuEFsSpxaESpjBRQAc1HE48qVTqMoSosEw8EYYADc19APZmANpxAsAvvnwB6J6lorQKMAAtdyOFBQcACeJJj+IP6BIchmMMgyciDy4e5gCL7JyD4oAA5QZrnQYKEgEhDhcWhY0PjWYMj5hcXAFcgAvMgA2gC6-uFwIMG9-cgAFADWEMFVGNhQAJS9LHUQDdTzlHM4nbh2o4PBTMgubh4gXr7hjArIMGsZEKR3BbT+yKRccPRw2KsNCDMIES1AMTxqUC2EI6eH2vwiR3wMDMUHG9UaBVysXiTSKUEMFQWeFYREBwNkoO4EK6uL6nQ20BpmJ69iAA
3.3: (Working)
https://www.typescriptlang.org/play/?ts=3.3.3#code/JYOwLgpgTgZghgYwgAgJIFUDO1kG8BQyRywAJgFzIgCuAtgEbQDchxCUEckpAgmJQBEuEFsSpxaESpjBRQAc1HE48qVTqMoSosEw8EYYADc19APZmANpxAsAvvnwB6J6lorQKMAAtdyOFBQcACeJJj+IP6BIchmMMgyciDy4e5gCL7JyD4oAA5QZrnQYKEgEhDhcWhY0PjWYMj5hcXAFcgAvMgA2gC6-uFwIMG9-cgAFADWEMFVGNhQAJS9LHUQDdTzlHM4nbh2o4PBTMgubh4gXr7hjArIMGsZEKR3BbT+yKRccPRw2KsNCDMIES1AMTxqUC2EI6eH2vwiR3wMDMUHG9UaBVysXiTSKUEMFQWeFYREBwNkoO4EK6uL6nQ20BpmJ69iAA
Related Issues:
The text was updated successfully, but these errors were encountered: