Syntax
value as Type;
value satisfies Type;Examples
as - Overriding Inference
Asserting a more specific type than TypeScript would infer on its own.
const input = document.getElementById("email") as HTMLInputElement;
input.value = "test@example.com"; // .value only exists on HTMLInputElement, not the general HTMLElement
const data = JSON.parse(jsonString) as { name: string; age: number };satisfies - Validate Without Widening
Checking a value matches a type while keeping its most specific inferred type.
type Colors = Record<string, string>;
const palette = {
red: "#ff0000",
green: "#00ff00"
} satisfies Colors;
// With satisfies, palette.red is still known as the literal "#ff0000"
// With a plain type annotation (: Colors), it would widen to just "string"
console.log(palette.red.toUpperCase()); // still gets full autocomplete on the specific keysBest practices
- Use as sparingly - it disables some of TypeScript's checking, so an incorrect assertion can hide real bugs rather than catch them
- Prefer satisfies over as when you want validation against a type while preserving the most specific inferred type of the value itself
- Never use as to force incompatible types together - if truly necessary, assert to unknown first (value as unknown as Type), which signals the risk explicitly
- Reach for a type guard (like a typeof or instanceof check) instead of as whenever runtime validation is genuinely possible
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.