Accepted answer
const [user, setUser] = useState(null);

Since you havn't given this a type, typescript has to try to infer it. It sees you passed in a null, so it assumes this state is (and always will be) null. Instead, you need to specify the type, as in:

interface UserData {
  username: string;
  password: string;
  prevState: null

const [user, setUser] = useState<UserData | null>(null);


If you fetching data from API you need this:

import React, { useState, useEffect } from 'react'
import axios from 'axios';

const MyComponent = () => {
const [data, setData] = useState<any | null>(null);
const fetchData = () => {
(...) //Axios.get() or async if u await data.
useEffect(() =>{
}, [])

Related Query

More Query from same tag