This code fails to compile:
type Validator<T> = (value: string) => { value: T }
const createValidator = <TState extends any>(state: TState) =>
<TName extends keyof TState>(x: TName, validator: Validator<TState[TName]>) => {
return (value: { [P in TName]: string }): { [P in TName]: TState[TName] } => {
const result = validator(value[x]);
return { [x]: result.value };
};
}
return { [x]: result.value }; gives me Type '{ [x: string]: TState[TName]; }' is not assignable to type '{ [P in TName]: TState[TName]; }'. even though TName is inferred from x.
Why is that? And what can I do about it - except from casting the returned value?
Validatordepends on other things we don't have access to. You might want to clear that up either by defining those (and making sure that they doesn't point to further third-party types we don't have) or, even better, by replacing them with built-in types.