I'm facing an issue with a code snippet like the one below:
interface IFoo {
bar: string;
baz: number;
}
function f(foo: IFoo, name: 'bar' | 'baz', val: any) {
foo[name] = val; // <<< error: Type 'any' is not assignable to type 'never'.
}
Surprisingly, when I change the type of "baz" to also be "string", the error disappears:
interface IFoo {
bar: string;
baz: string;
}
function f(foo: IFoo, name: 'bar' | 'baz', val: any) {
foo[name] = val; // fine
}
I am curious about the reason behind this behavior and whether there's a solution that doesn't involve changing name: 'bar' | 'baz'
to name: string
.