Why understanding TypeScript matters when using AI
You ask AI for an Order type from your API response. It gives you:
type Order = {
id: string
amount: number
status: string
discount?: number
}
It compiles. Everything looks good.
The real system only sends four values: 'pending' | 'paid' | 'cancelled' | 'refunded'.
Six weeks in, someone writes if (order.status === 'canceled') with one L.
The code compiles, but the comparison never matches. As a result, the system no longer refunds cancelled orders. You only find out because a customer tells you about the problem, not because the compiler caught it.
Also, is the amount in cents or in rupees?
The type signature does not specify. Nothing stops you from adding a value in cents to a value in rupees. A branded type would make this mistake a compile error: type Cents = number & { readonly brand: unique symbol }.
Then there’s discount?: number.
The question mark means that...
