as vs satisfies vs type annotations overview
All three relate an expression to a type. They differ in what the compiler verifies and in what type the expression ends up with afterward.
Type annotation
const x: T = expr;
expr must be assignable to T, and the binding’s type becomes T. Whatever the compiler inferred about expr beyond T is discarded. Fresh object literals get excess property checks.
type Route = { path: string; auth?: boolean };
const routes: Record<string, Route> = {
home: { path: "/" },
admin: { path: "/admin", auth: true },
};
routes.home; // Route
routes.hmoe; // Route (no error: any string key is valid)
The annotation is a contract on the binding. Use it when the declared type is the type you want consumers to see.
Type assertion: as
const x = expr as T;
The compiler treats expr as T without proving it. An assertion from S to T is allowed only if S is assignable to T or T is assignable to S. It emits no runtime code, and excess property checks do not run.
const input = document.querySelector("#email") as HTMLInputElement;
// Element | null -> HTMLInputElement. Unchecked: null or a non-input still passes.
const cfg = {} as Config;
// Compiles. Required fields are absent at runtime.
const a = "5" as number;
// error TS2352: types do not sufficiently overlap
const b = "5" as unknown as number;
// compiles; the compiler has been overridden
Because the result is unchecked, as is the only one of the three that can produce an unsound type. A later refactor that changes the real shape of the value will not be reported at the assertion.
satisfies
const x = expr satisfies T;
Added in TypeScript 4.9. expr must be assignable to T, as with an annotation, but the resulting type is the inferred type of expr, not T. T is also used as the contextual type, so literals, tuples, and callback parameters are typed against it.
const routes = {
home: { path: "/" },
admin: { path: "/admin", auth: true },
} satisfies Record<string, Route>;
routes.admin.auth; // boolean
routes.home.auth; // error: property 'auth' does not exist
routes.hmoe; // error: property 'hmoe' does not exist
Unions are narrowed to the member that was actually written:
type Color = string | [number, number, number];
const palette = {
red: [255, 0, 0],
green: "#0f0",
} satisfies Record<"red" | "green", Color>;
palette.red.map(c => c / 255); // ok: red is the array member
palette.green.toUpperCase(); // ok: green is the string member
// With `const palette: Record<"red" | "green", Color> = ...`
// both calls above are errors, because each property is the full union.
Callback parameters are contextually typed without being annotated:
const inc = (x => x + 1) satisfies (x: number) => number; // x: number
The inferred type is what the variable holds, so it may be narrower than you want to assign later:
let v = "a" satisfies string | number;
v = 1; // error: v is string
Comparison
| Annotation | as | satisfies | |
|---|---|---|---|
| Compile-time check | Assignable to T | Comparable to T (either direction) | Assignable to T |
| Resulting type | T | T | Inferred type of the expression |
| Excess property check | Yes (fresh literals) | No | Yes (fresh literals) |
| Contextual typing | Yes | Yes | Yes |
| Can be unsound | No | Yes | No |
| Runtime effect | None | None | None |
| Applies to | Bindings, parameters, returns, fields | Any expression | Any expression |
| Minimum TypeScript | Any | Any | 4.9 |
as const satisfies
as const is a const assertion, not an assertion to a type: it makes literals readonly and non-widened. It combines with satisfies to validate a constant against a shape while keeping literal types.
const routes = {
home: "/",
about: "/about",
} as const satisfies Record<string, `/${string}`>;
type Path = (typeof routes)[keyof typeof routes]; // "/" | "/about"
T must accept readonly values. For arrays, use readonly string[] rather than string[].
Choosing
- Annotation for function parameters and return types, exported values, class fields, and variables that will be reassigned. The declared type is the intended type.
satisfiesfor object literals, config, lookup tables, and constants that must conform to a type while keeping their specific shape: known keys, literal values, per-property narrowing.asonly when you hold information the compiler cannot derive: DOM queries, values already validated at a boundary, test fixtures. Prefer a type guard or a schema validator when the value comes from outside the program. Avoidas unknown as Tin production code.