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
exportconstKEYS=['cat','dog','fox','orca','ox']asconstexportconstKEYS_TYPE_MAPPING={cat: 'options',dog: 'options',fox: 'options',orca: 'range',ox: 'range',}asconstexporttypeKeyValue={options: string[]range: {min?: number;max?: number}}exporttypeItems={[Kin(typeofKEYS)[number]]?: KeyValue[(typeofKEYS_TYPE_MAPPING)[K]]}letanimals: Items={cat: ["x","y","x"]}for(letkeyofKEYS){// union of possible value types in 'KeyValue'letx=animals[key]// ERROR: becomes an intersection when trying to set the valueanimals[key]=x}
π Actual behavior
After creating a object type where each key has a mapped value type, an intersection of said types is created which causes object[key] to not be the same type as object[key] when setting a value at an index of the object.
It should not be an intersection type since the value types never overlap.
π Expected behavior
I expected the type of 'x' (when setting it as the value of object[key]) to be the same type as 'object[key]'. Since they both reference the same index in the same object. Also I would expect the type of 'x' and 'object[key]' to be a union of the value types since they do not overlap.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
See #55756 (comment) and #30769. When key is union-typed, as it is here, the compiler doesnβt understand that key refers to the same property in both places. The intersection type arises because thatβs the only type thatβs known to be valid for any possible key.
Interesting, I assume the most optimal work around would be to explicitly type each key value as the union then? Unless there's something else that I'm not aware of.
π Search Terms
"indexing a conditional type object", "type mismatch indexing a conditional type object ts"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwgOwM7wNIFECaBlOAXjgG0ByBAQxjIBo4yATCAczoYDMIR2zore3MgF04lVIhToAsACg5oSLElpMuPAH0AKjgAKWDQFkAgrt0BJAHIBxInADecuImoAuBhDAwAllLpO4ZhZ3Pi9fNH9ZZy4QEM8fP1oA-koQqEpkFmBI5240jKzIgF8xCSRVOQVwaHgYAE8wYDgMYDqANUoAGwBXJuJHKLh48NR3dChvTJJhAPTM4Hd7OABbSYB+d2Ru5YAjYCgAbhXKEA24Ld39uCK5G-lZRRq4esa4cxhgZYl+gJIMOEmcAAFC9gBAOM11ABKEgXPZQYTCM4tdpdXokEENMEQ7D4bR6AwmMxWawwjCI26Ve6dYDwDLeZZdUZvD5fOz2KgwdwkABEIB59B5dQFcD5POEdzkXCgwJp8AA1q0hjjoQ4AgB6dVwbrIcLKuCQVCobw7GlwABuaKaoIkgLIKI6PWyATlcBAdnpjM6qBIirqM3uzk1cCwACVQwB5UPuPZIZbACQZAHID5QVDABAJZBwADuAAtgNmYFA6pMWM8IHB07UCxarQFPUzfa1RMQQJTZEA
π» Code
π Actual behavior
After creating a object type where each key has a mapped value type, an intersection of said types is created which causes object[key] to not be the same type as object[key] when setting a value at an index of the object.
It should not be an intersection type since the value types never overlap.
π Expected behavior
I expected the type of 'x' (when setting it as the value of object[key]) to be the same type as 'object[key]'. Since they both reference the same index in the same object. Also I would expect the type of 'x' and 'object[key]' to be a union of the value types since they do not overlap.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: