Let's say we have a React application with TypeScript. When using the useState hook, it can be confusing where to add the type declaration. In our example, the held data will be an array of users. So firstly, we will create a typing for a single user like this:
export type User = { _id: string; userName: string; userRole: string; password: string; }
Our useState hook for holding the user's array currently looks like this:
const [allUsers, setAllUsers] = useState([]);
To tell TypeScript, that "allUsers" will hold an array of users, just change your code to be the following:
const [allUsers, setAllUsers] = useState<User[]>([]);