I am facing an issue with the following code snippet:
interface A {
z: string;
y: number;
}
let newA = <T, S extends keyof T>(key: S, value: T[S]): Partial<T> => {
return {[key]: value};
}
Upon running this code, I encounter an error message:
Type '{ [x: string]: T[S]; }' is not assignable to type 'Partial<T>'
It seems like the problem lies in the {}
part, as it always maps key
to be a string
Even when explicitly specifying that it should be a string
, I am unable to resolve this issue
The only workaround I found was to remove Partial<T>
, but that is not an ideal solution
I am seeking guidance from someone well-versed in typescript
at an expert level who can explain the underlying mechanism of this problem and provide a viable solution
Important! This issue is specific to the generic case. It works fine without generics.