score:6

Accepted answer

You need TS to be able to infer at the time that you call useState what sort of value it will contain.

Since it's going to be an array of Users, use that for the generic:

const [users, setUsers] = useState<User[]>([]);

I'd also highly recommend not ignoring errors - unhandled rejections should be avoided whenever possible.

fetchUsers()
  .then(setUsers)
  .catch(handleErrors);

You also probably only want to fetch the users once, rather than every time the component mounts.

useEffect(() => {
  fetchUsers()
    .then(setUsers)
    .catch(handleErrors);
}, []);

Related Query

More Query from same tag