Description
TypeScript Version: Reproduced in the playground - i assume this is running latest. before running in the playground was running on 3.4.5
Search Terms:
keys across objects cannot be compared
type values at same indexes do not honor that keys can only assume one value at a time
(I'm having a hard time describing this issue, so I may have failed to search for it effectively; forgive me if that's the case.)
Code
type A = { a: 'a' | 'b'; b: 'c' | 'd'; };
type B = { a: ('a' | 'b')[]; b: ('c' | 'd')[]; };
type C = { a: string; b: number };
type D = { a: string[]; b: number[]; }
const a: A = { a: 'a', b: 'c' };
const b: B = { a: [], b: [] };
const c: C = { a: 'asdf', b: 1234 };
const d: D = { a: [], b: [] };
(['a', 'b'] as const).forEach(key => {
b[key].push(a[key]);
b[key] = [...b[key], a[key]];
d[key].push(c[key]);
d[key] = [...d[key], c[key]];
});
Expected behavior:
All of the options for adding the keys to the array in the second half of the example above should compile and work correctly.
Actual behavior:
The LHS of the equation is considered an &
type while the RHS is an |
type, which makes them incompatible. These types are clearly correct and should work, but e.g. b[key]
is consider a never
for the purposes of push ('a' | 'b' & 'c' |'d'
), while d[key]
is considered a string & number
for those purposes (not sure why this isn't also a never
)
The important part of this issue is that none of the possible ways of using these types together compile. All possible variants create too strict of a typing for what needs to go into the array variant of the original type, even though the types of the objects at identical keys are compatible
Playground Link:
Related Issues: