Description
TypeScript Version: 3.7.0-dev.20190928
Search Terms:
- any assigned to never
- reduce any to never
- cannot reduce to interface
Code
Ever since my team has updated to v3.6.3, the introduction of never
has been causing us headaches whenever we try to reduce an array into a single typed object. The following example illustrates the issue we're faced with.
interface Thing {
foo: number,
bar: string,
}
const list: Array<[keyof Thing, any]> = [['foo', 1], ['bar', 'baz']];
const result: Thing = list.reduce((obj: Thing, [key, value]) => {
// Compile-time error is thrown here because `any` is being assigned to `never`.
obj[key] = value;
return obj;
}, { foo: 0, bar: ''});
My best guess is that this behaviour is due to recent changes in TS 3.5 to fix unsound writes to indexed access types. While understandable, I'm having some difficulty trying to reason about how we might achieve the behaviour demonstrated above without type aliasing obj as any
or something similar? My gut tells me this should be achievable without breaking out of the type system (i.e. aliasing), which leads me to believe this could potentially be a bug.
Expected behavior:
value
of type any
is allowed to be set as a property of Thing
.
Actual behavior:
The following compile-time error is thrown.
Playground Link:
Related Issues:
N/A