Lesson 113 of 296
satisfies, as const, and enums vs unions
What you will learn
- Type assertions with
as, and their dangers as const- The
satisfiesoperator - Enums: how they work and their problems
- Better alternatives to enums
On this page
Type assertions: as
as tells TypeScript "treat this value as this type". It does not check or convert anything at runtime.
const input = document.getElementById("email") as HTMLInputElement;
input.value;Here it is reasonable: you know that element is an input.
The danger:
const user = JSON.parse(text) as User; no real check: if the data is wrong, bugs followTypeScript allows assertions between types that overlap. For unrelated types it refuses, unless you go through unknown (x as unknown as Y), which is a strong warning sign in code review.
Prefer narrowing (Lesson 106) or validation (Lesson 114) to as.
as const
as const makes TypeScript infer the narrowest possible type: literal values and readonly properties.
const config = { env: "production", retries: 3 };
type: { env: string; retries: number }
const config2 = { env: "production", retries: 3 } as const;
type: { readonly env: "production"; readonly retries: 3 }Arrays become readonly tuples:
const roles = ["admin", "editor", "viewer"] as const;
type: readonly ["admin", "editor", "viewer"]
type Role = (typeof roles)[number]; "admin" | "editor" | "viewer"This gives you a runtime list (for a dropdown or validation) and a type from one source.
satisfies
satisfies checks that a value matches a type, without changing the value's inferred type.
The problem it solves:
type Colors = Record<"primary" | "danger", string | [number, number, number]>;
const colors: Colors = {
primary: "#1f5fbf",
danger: [220, 38, 38]
};
colors.primary.toUpperCase(); Error: could be a tupleWith an annotation, TypeScript forgets the specific value types.
const colors = {
primary: "#1f5fbf",
danger: [220, 38, 38]
} satisfies Colors;
colors.primary.toUpperCase(); fine: still known to be a string
colors.danger[0]; fine: still known to be a tupleAnd it still catches mistakes:
const bad = { primary: "#fff" } satisfies Colors; Error: 'danger' is missingAnnotation : Type | satisfies Type | as Type | |
|---|---|---|---|
| Checks the value matches | Yes | Yes | Barely (overlap only) |
| Resulting type | The annotation's type (wider) | The value's own inferred type | The asserted type |
| Safe | Yes | Yes | No |
Common use: configuration objects, route maps, theme tokens. With as const:
const routes = {
home: "/",
orders: "/orders"
} as const satisfies Record<string, `/${string}`>;Enums
TypeScript has enum, a feature that JavaScript does not:
enum Status {
Pending,
Paid,
Shipped
}
Status.Paid; 1Numeric enums start at 0 and also create a reverse mapping: Status[1] is "Paid".
String enums:
enum Role {
Admin = "ADMIN",
Editor = "EDITOR"
}Problems with enums
- Most TypeScript features are erased, but enums generate real JavaScript code (an object). This breaks the simple "TypeScript is JavaScript plus types" model and is not supported by type-stripping tools such as Node.js's built-in TypeScript support.
- Numeric enums accept unexpected numbers in some situations.
- String enums are nominal: you cannot pass the plain string
"ADMIN"whereRoleis expected, which is awkward with API data. const enumavoids the generated object but has its own build-tool problems.
Better alternatives
A union of string literals:
type Role = "admin" | "editor" | "viewer";An as const object, when you also need the values at runtime:
const ROLE = {
Admin: "admin",
Editor: "editor",
Viewer: "viewer"
} as const;
type Role = (typeof ROLE)[keyof typeof ROLE]; "admin" | "editor" | "viewer"
function setRole(role: Role) {}
setRole(ROLE.Admin);
setRole("admin"); also fineMany style guides now recommend unions or as const objects over enums. You will still meet enums in existing code, Prisma schemas and NestJS projects, so know how they work.
Common mistakes
- Using
asto silence errors. - Using
as unknown as X. - Annotating a config object and losing its specific types, when
satisfieswould keep them. - Assuming enums are erased like other types.
How to say it in an interview
"Type assertions with as only change what the compiler believes, with no runtime check, so I prefer narrowing or validation. as const infers the narrowest literal and readonly types, so I can derive a union from a runtime array or object. satisfies checks a value against a type while keeping its precise inferred type, which suits configuration objects. Enums generate runtime code and string enums are awkward with plain strings, so I usually prefer string literal unions or as const objects, though I can work with enums in existing codebases."
Key words
| Word | Meaning |
|---|---|
| Type assertion | as: telling the compiler a type without checking |
| as const | Infer the narrowest, readonly literal types |
| satisfies | Check a value against a type without widening it |
| Enum | A TypeScript feature that creates named constants at runtime |
| Reverse mapping | Numeric enums mapping values back to names |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.