I have custom hook that adds to useStata function that accepts key and value props;
import { Dispatch, SetStateAction, useCallback, useState } from 'react';
export type HandleModelChangeFn<T> = (key: keyof T, value: T[typeof key]) => void;
const useModel = <T>(initialModel: T): [T, HandleModelChangeFn<T>, Dispatch<SetStateAction<T>>] => {
const [model, setModel] = useState<T>(initialModel);
const handleChange = useCallback((key: keyof T, value: T[typeof key]) => {
setModel((prevState) => ({
...prevState,
[key]: value,
}));
}, []);
return [model, handleChange, setModel];
};
export default useModel;
Problem is that when i use that function second parameter can be any type which exists in interface, example
interface User {
name: string;
age: number;
}
const initialModel: User = {
name: '',
age: 1
}
const [model, handleModelChange] = useModel(initialModel);
handleModelChange('age', 'John'); // should get error cause value is wrong type
key param can be 'name' or 'age', but second param has type string | number;
I want to calculate type dynamically based on key property, like when key param is 'age' ts will tell me that i passing wrong value type, cause age type is number;
Is there way to do this with typescript?