medium10 pts · first answer countstypescriptapi-contracts
Type cast on fetch response
The backend renamed fullName to displayName. tsc still passes, but the frontend crashes in production. Why didn’t TypeScript catch it?
type User = { id: string; fullName: string }
const res = await fetch('/api/me')
const user = (await res.json()) as User
render(user.fullName.toUpperCase())Discussion 0
Join free to post in the discussion.
Related questions
- Optional property in strict modeFull Stack Developer
- One schema for form and APIFull Stack Developer
- Choosing an API contract approachFull Stack Developer
- TypeScript types at runtimeFrontend Developer
- unknown versus anyFrontend Developer
- Exhaustive check with neverFrontend Developer
- When extra properties are an errorFrontend Developer