'How to create a generic object property updater using TypeScript

I am trying to work out how to create a generic object property updater which is properly typed in TypeScript

const [originalObj, setOriginalObj] = useState<MyModel>(defaultModel)

const updateModelProp = <T extends keyof MyModel> (key: T, value: any) => {
  const newModel = { ...originalObj, [key]: value }
  setOriginalObj(newModel)
}

I have it working, other than the 'value' column, where I want it to use TypeScript to ensure you are using the correct type to the model provided.

Can anyone help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source