Sub topic 10
Limitations: no runtime checks and other pitfalls
Assume you know basic TypeScript types, unions, object literals, Record, and type inference. This chapter focuses on what satisfies cannot do. The operator is useful because it checks types without replacing the inferred type, but it only works during compilation.
`satisfies` does not validate runtime data
TypeScript removes type information when it compiles to JavaScript. The satisfies operator also disappears. It cannot inspect data from a user, a file, an API, or an environment variable while the program runs.
type Config = {
port: number;
host: string;
};
const config = {
port: 8080,
host: "localhost",
} satisfies Config;
console.log(config.port); // 8080The object literal is checked while TypeScript compiles the file. At runtime, JavaScript receives an ordinary object. There is no check that prevents this JavaScript value from being used:
const externalData: unknown = JSON.parse('{"port":"not a number"}');
const config = externalData as Config;
console.log(config.port + 1); // "not a number1"The as Config expression does not convert the string to a number. It only tells TypeScript to trust the programmer. satisfies cannot safely replace validation for untrusted data.
Use a runtime validation library or write an explicit validation function at a trust boundary. A trust boundary is the point where data enters your program from outside it.
flowchart LR
A[External JSON] --> B[Runtime validation]
B -->|valid data| C[Typed application code]
B -->|invalid data| D[Error handling]
C --> E[satisfies checks internal literals at compile time]caption: Compile-time and runtime checks happen at different stages alt: External data enters runtime validation before typed application code; satisfies checks internal literals during compilation
It checks the expression you give it
satisfies checks an expression at its location. It does not permanently attach a runtime rule to a variable or to future mutations.
type Settings = {
mode: "development" | "production";
};
const settings = {
mode: "development",
} satisfies Settings;
let mode: string = settings.mode;
mode = "anything"; // This is allowed because mode is a separate string variable.
console.log(settings.mode); // developmentIn this example, the original object remains valid. The later variable mode has type string, so it can hold any string. satisfies does not track every copy of a value through the program.
The operator also does not make an object immutable. If its inferred properties are mutable, later assignments are checked against those inferred types, not against the original satisfies expression.
type Palette = Record<"primary" | "secondary", string>;
const palette = {
primary: "#3366ff",
secondary: "#ffffff",
} satisfies Palette;
palette.primary = "#000000"; // Allowed: it is still a string.If a value must keep exact literal values and resist mutation, combine as const with satisfies.
`as const` changes mutability
as const preserves literal values and makes object properties and arrays readonly. The target type used with satisfies must allow those readonly values.
type Tags = readonly string[];
const tags = ["typescript", "static-checking"] as const satisfies Tags;
console.log(tags[0]); // typescriptThe type of tags is readonly ["typescript", "static-checking"]. This will fail because a readonly tuple cannot be assigned to a mutable array type:
// const tags = ["typescript"] as const satisfies string[];Use readonly in the target type when the expression contains as const arrays or tuples.
as const can also make object properties readonly:
type Endpoint = {
method: "GET" | "POST";
path: `/${string}`;
};
const endpoint = {
method: "GET",
path: "/users",
} as const satisfies Endpoint;
// endpoint.method = "POST"; // Error: the property is readonly.This combination is useful for route tables, command definitions, and other constant data. It is not appropriate when the application needs to update the object.
Excess-property checks have limits
With an object literal, satisfies usually catches an extra property:
type Options = {
port: number;
};
const options = {
port: 8080,
// porrt: 8080, // Error: unknown property if this typo is added.
} satisfies Options;However, excess-property checking is not a general-purpose object inspection system. A value that has already been stored in a wider variable can pass through another type more easily:
type Options = {
port: number;
};
const source = {
port: 8080,
debug: true,
};
const options: Options = source;
console.log(options.port);The assignment is allowed because source has at least the required port property. The debug property still exists at runtime, but it is not part of the static type Options.
This distinction matters when designing APIs. satisfies is especially effective directly on configuration literals. It does not guarantee that every object later passed to a function has exactly the target shape.
It cannot fix an incorrect type model
satisfies validates against the type you write. If that type is too broad or expresses the wrong rule, the check can still succeed.
type Feature = {
enabled: boolean;
};
const feature = {
enabled: "yes",
} satisfies {
enabled: string | boolean;
};
console.log(feature.enabled); // yesThe expression satisfies the written type, but it may violate the application's intended meaning. If enabled must be a boolean, the target type must say so:
type StrictFeature = {
enabled: boolean;
};
const strictFeature = {
enabled: true,
} satisfies StrictFeature;Satisfies improves the connection between values and types. It does not replace careful type design.
Choosing a safer boundary
The following table summarizes the division of responsibility:
| Need | Suitable tool |
|---|---|
| Preserve a precise inferred type while checking a literal | `satisfies` |
| Tell TypeScript to trust a value without checking it | `as` |
| Preserve literal values and make them readonly | `as const` |
| Check unknown runtime data | Runtime validation |
| Require exact keys in a source object literal | `satisfies` with `Record` or an object type |
A common pattern is to validate external data first, then use precise types inside the application. For source-defined constants, as const satisfies often provides the strongest static result.
Exercise
Why does const config = externalData as Config not protect the program when externalData comes from JSON?
Show answer
as Config changes only TypeScript's static view. It performs no runtime conversion or validation, so invalid JSON can still contain values such as a string where Config requires a number.