Type for a JSON object or API response?

Is Object good enough?
7 Replies
rocawear
rocawear2y ago
Good enought for who? It does not give you any typesafety
niels
nielsOP2y ago
Thats what I thought yeah. What would be a good type for JSON values?
rocawear
rocawear2y ago
Values that you have on the data
niels
nielsOP2y ago
Yeah but I mean mostly API responses. Should I really type every response and cast it to axios.get<Type> for example?
jermberj
jermberj2y ago
The more you type things now, the less pain you will feel later.
whatplan
whatplan2y 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
whatplan2y 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