I'm attempting to modify certain fields of my object using field names. Here is the code snippet I have written:
interface Foo {
a: number[],
b: string[],
}
type Bar = { [T in keyof Foo] : (arg : Foo[T]) => Foo[T] }
function test<T extends keyof Foo>(field: T) {
const foo : Foo = {
a: [],
b: [],
};
const bar: Bar = {
a: arg => /* some code */ [],
b: arg => /* some code */ [],
};
foo[field] = bar[field](foo[field]);
}
However, when executing bar[field](foo[field])
, I receive the following error message:
Argument of type 'Foo[T]' is not assignable to parameter of type 'number[] & string[]'.
Type 'number[] | string[]' is not assignable to type 'number[] & string[]'.
Type 'number[]' is not assignable to type 'number[] & string[]'.
Type 'number[]' is not assignable to type 'string[]'.
Type 'number' is not assignable to type 'string'.
Type 'Foo[T]' is not assignable to type 'number[]'.
Type 'number[] | string[]' is not assignable to type 'number[]'.
Type 'string[]' is not assignable to type 'number[]'.
Type 'string' is not assignable to type 'number'
Shouldn't TypeScript infer that with the same T
, Foo[T]
and Parameters<Bar[T]>
should hold the same value?