What is as const in TypeScript?

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.