What is as const in TypeScript?
Why Interviewers Ask This
This is a classic screening question for TypeScript roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
The as const assertion (const assertion) tells TypeScript to treat an expression as a deeply immutable literal type rather than widening it to a general type. Without as const: const config = { port: 3000 } — TypeScript infers { port: number }. With as const: const config = { port: 3000 } as const — TypeScript infers { readonly port: 3000 } (the literal value 3000, not the general type number). For arrays: const directions = ["North", "South"] as const — type is readonly ["North", "South"], not string[]. This is powerful for creating type-safe enums without the enum keyword: const STATUS = { Active: "active", Inactive: "inactive" } as const; type Status = typeof STATUS[keyof typeof STATUS]; — gives you "active" | "inactive". as const makes all nested properties readonly and infers the most specific types possible.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.