I\'m migrating a React with TypeScript project to use hooks features (React v16.7.0-alpha), but I cannot figure out how to set typings of the destructured elements.
First useState takes a generic, which will be your IUser. If you then want to pass around the second destructured element that is returned by useState you will need to import Dispatch. Consider this extended version of your example that has a click handler:
import React, { useState, Dispatch } from 'react';
interface IUser {
name: string;
}
export const yourComponent = (setUser: Dispatch) => {
const [user, setUser] = useState({name: 'Jon'});
const clickHander = (stateSetter: Dispatch) => {
stateSetter({name : 'Jane'});
}
return (
)
}
See this answer.