type X = {
aa: string;
bb: number;
};
const get = <Key extends keyof X, Value extends X[Key]>(
key: Key,
value: Value | ((v: Value) => Value)
) => {
let newValue: Value;
const x: X = {
aa: '11',
bb: 11
};
if (typeof value === 'function') {
//Issue with type assignment: 'Argument of type 'X[Key]' is not assignable to parameter of type 'Value'.
newValue = value(x[key]);
} else {
newValue = value;
}
};
What could cause the error message "Argument of type 'X[Key]' is not assignable to parameter of type 'Value'?" When looking at it from a logical perspective X[Key]
should match what Value
represents. So why does TypeScript interpret it as not assignable? As a person analyzing the types, it seems impossible to make an error in type determination. Why is TypeScript unable to recognize this?