Type for a JSON object or API response?

Is Object good enough?
7 Replies
rocawear
rocawear12mo ago
Good enought for who? It does not give you any typesafety
niels
niels12mo ago
Thats what I thought yeah. What would be a good type for JSON values?
rocawear
rocawear12mo ago
Values that you have on the data
niels
niels12mo ago
Yeah but I mean mostly API responses. Should I really type every response and cast it to axios.get<Type> for example?
jermberj
jermberj12mo ago
The more you type things now, the less pain you will feel later.
whatplan
whatplan12mo ago
generic casting is better than nothing but there still no guarentee that what comes back is actually the type your telling typescript it is use zod: https://zod.dev/ to validate your data and make sure it matches what you expect
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
whatplan
whatplan12mo ago
its as easy as defining a schema and parsing your data what your get back will be fully typed as infered from your schema
Want results from more Discord servers?
Add your server
More Posts