The problem is when you define the userInfo
variable. You are using the following code:
const [userInfo, setUserInfo] = useState([]);
Often when we write TypeScript, we take advantage of the powerful compiler. It will implicitly type variables upon declaration. This can be useful when you initialize a variable with a string
, number
, boolean
, or a prototype/class constructor. The downside to this is that, oftentimes, we assign a variable, but never check what data type TypeScript implicitly assigned to it.
In this case, you userInfo
variable is implicitly assigned a never[]
type. Why? The definition for the useState()
function is as follows:
function useState<T>(value: T): [T, StateSetter] { ... }
The StateSetter
type is something I made up because I don't know the exact type (but it does not matter for this question)
What you're doing is passing in []
to useState
. Since this array has 0
elements, TypeScript will implicitly type this with never[]
(meaining that it is an array that will never have any elements. So in this case, T
is assigned to never[]
so the return type of useState
is [never[], StateSetter]
.
You can modify this by adding a type parameter to useState
:
interface UserInfo {
...
}
const [userInfo, setUserInfo] = useState<UserInfo[]>([]);
This tells TypeScript what kind of information can be stored in this array (if you were to ever add any elements).
NOTE FOR FUTURE POSTS: Do not include screenshots of your code. They are impossible to read, and people trying to help you cannot copy/paste your code. Include the code as formatted code in your question.
You can accomplish this by using the back-ticks:
This is regular text:
`This` will show up as inline code
```javascript
function thisWillShowUpAsJavaScriptCodeBlock() {
...
}
This is regular text:
This
will show up as inline code
function thisWillShowUpAsJavaScriptCodeBlock() {
...
}