mediumStudy question · answer published in JavaScript interview questions, so no pointstypescriptapi-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
- Type inferred from a letFrontend Developer
- Narrowing a union with typeofFrontend Developer
- Declaring the same interface twiceFrontend Developer
- Type of an optional parameterFrontend Developer