You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran the following code, I noticed that the type of data is string.
This behavior is normal for the fetch function.
However, in TypeScript, it is inferred as object, and when I run JSON.parse(data), I get a type error.
Why is this happening?
const { data } = await client.GET("/test")
console.log(typeof data) // string
console.log(data.userId) // undefined
const json = JSON.parse(data) // It works but type error
console.log(json.userId) // returns expected value
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I ran the following code, I noticed that the type of data is string.
This behavior is normal for the fetch function.
However, in TypeScript, it is inferred as object, and when I run JSON.parse(data), I get a type error.
Why is this happening?
Beta Was this translation helpful? Give feedback.
All reactions