Sub topic 10
Limitations: No Runtime Checks and Other Pitfalls
This chapter assumes you know the satisfies syntax, how annotations widen types, how as const works, and how excess property checks behave on object literals. You also need strict mode turned on, which is the setting all the examples assume.
satisfies is a compile-time tool. It solves one problem well: it checks an expression against a type and keeps the precise inferred type. It does not solve every problem near that one. This chapter covers where it stops, the mistakes people make when they expect more from it, and a short checklist for deciding when it is the wrong tool.
It exists only at compile time
The TypeScript compiler removes satisfies when it produces JavaScript. The check happens during type checking, and nothing about it remains in the output.
type Options = { port: number; host?: string };
const options = { port: 8080, host: "localhost" } satisfies Options;The emitted JavaScript is:
const options = { port: 8080, host: "localhost" };The same holds for annotations and as assertions. All TypeScript types are erased. The result is that satisfies can only vouch for values the compiler can see. An object literal written in your source file is visible. A string read from a file, a network response, or an environment variable is not.
Pitfall 1: external data is not checked
The most common mistake is using satisfies on data that arrives at runtime. JSON.parse returns any, and any is assignable to every type, so the check always passes.
type Config = { port: number; host?: string };
const text = '{"port": "not a number"}';
// Compiles without error: JSON.parse returns any.
const config = JSON.parse(text) satisfies Config;
console.log(config.port + 1); // prints "not a number1"Two things went wrong here. The check passed because the expression has type any. The resulting type of config is also any, because satisfies does not change the type of the expression. You get neither validation nor type safety afterward.
A property typed any inside a literal is also accepted:
type Config = { port: number; host?: string };
const config = { port: JSON.parse("\"oops\"") } satisfies Config; // compilesThe fix is to treat outside data as unknown and validate it at runtime. With unknown, satisfies becomes useful as a safety net, because it refuses to accept the value:
type Config = { port: number; host?: string };
function isConfig(value: unknown): value is Config {
if (typeof value !== "object" || value === null) return false;
const v = value as Record<string, unknown>;
return (
typeof v.port === "number" &&
(v.host === undefined || typeof v.host === "string")
);
}
function parseConfig(text: string): Config {
const data: unknown = JSON.parse(text);
if (!isConfig(data)) {
throw new Error("Invalid config");
}
return data;
}
console.log(parseConfig('{"port": 8080}')); // { port: 8080 }
try {
parseConfig('{"port": "not a number"}');
} catch (error) {
console.log((error as Error).message); // Invalid config
}Hand-written guards like isConfig work for small shapes. For larger ones, schema libraries such as Zod, Valibot or Ajv generate the checks and the TypeScript type from one description. Whichever you choose, the rule is the same: types describe what you believe about a value, and a runtime check is what makes the belief true.
Pitfall 2: excess property checks need a fresh literal
You saw that satisfies catches misspelled properties in object literals. That check applies only to a fresh object literal, meaning a literal written directly in the expression. When you check a variable that already holds an object, TypeScript uses plain assignability, and extra properties are allowed.
type Options = { port: number; host?: string };
const draft = { port: 80, hots: "localhost" };
// No error: draft is not a fresh literal, so only assignability is checked.
const checked = draft satisfies Options;
// Error: 'hots' does not exist in type 'Options'.
const direct = { port: 80, hots: "localhost" } satisfies Options;This matters when you build a configuration in steps, or when you spread one object into another. Spread properties are not checked for excess either:
type Options = { port: number; host?: string };
const base = { port: 80, hots: "localhost" };
// No error: properties that come from a spread are not excess-checked.
const merged = { ...base } satisfies Options;If you need protection against typos, keep the literal and the satisfies in the same expression, and avoid routing the data through an intermediate variable first.
Pitfall 3: the inferred type can be narrower than you want
satisfies keeps the inferred type of the expression. For a mutable object, that inferred type is usually the widened type of each property, such as number for 3. It is not the type you wrote after satisfies. This surprises people when they later assign to a property.
type Settings = { retries: number | undefined };
let settings = { retries: 3 } satisfies Settings;
// Error: Type 'undefined' is not assignable to type 'number'.
settings.retries = undefined;The satisfies type allowed undefined, but settings has the inferred type { retries: number }. The check happened once, at the declaration. Later writes are checked only against the inferred type.
If you want the variable to accept every value in the wider type, an annotation is the correct tool. You lose per-property precision, but you gain the freedom to assign later. Choosing between the two is the subject of the previous chapter, and this case is a clear example of where the annotation wins.
Pitfall 4: literals survive only when the context asks for them
satisfies provides a contextual type, and that context decides whether string and number literals are kept or widened. A literal union in the context preserves the literal. A plain string does not.
type Shape = { kind: "circle" | "square" };
const shape = { kind: "circle" } satisfies Shape;
// shape.kind has type "circle"
const loose = { kind: "circle" } satisfies { kind: string };
// loose.kind has type stringA frequent version of this mistake involves a Record<string, string> constraint. You hope to derive a union of the values, but the context says only string, so the values widen:
const routes = {
home: "/",
user: "/users/:id",
} satisfies Record<string, string>;
type Route = (typeof routes)[keyof typeof routes]; // string, not "/" | "/users/:id"Adding as const fixes this, as the earlier chapter showed. You can then tighten the constraint, for example to ` Record<string, /${string}> `, and get both exact values and validation:
const routes = {
home: "/",
user: "/users/:id",
} as const satisfies Record<string, `/${string}`>;
type Route = (typeof routes)[keyof typeof routes]; // "/" | "/users/:id"Pitfall 5: `as const` makes everything readonly
Adding as const has side effects beyond literal types. Every property becomes readonly, and every array becomes a readonly tuple. The type after satisfies must accept that, or you get an error.
// Error: the readonly tuple is not assignable to string[].
// const bad = ["a", "b"] as const satisfies string[];
const ok = ["a", "b"] as const satisfies readonly string[];
// ok has the type readonly ["a", "b"]The same applies to nested arrays in an object. If the constraint type declares string[] anywhere, as const will clash with it, and you must change the declaration to readonly string[]. Changing the constraint is correct here. Removing as const to silence the error would throw away the literal types you wanted.
Remember also that readonly is a compile-time property. Nothing freezes the object at runtime. If you need runtime immutability, call Object.freeze yourself, and note that it is shallow.
Pitfall 6: assignability rules still apply
satisfies uses the same compatibility rules as ordinary assignment, including the permissive ones. For functions, a function that declares fewer parameters than the target is accepted, because the caller can safely ignore extra arguments:
type Handler = (event: string, index: number) => void;
const handler = ((event: string) => {
console.log(event);
}) satisfies Handler;
handler("click"); // fineThis is correct behavior, but it means a handler that ignores an argument you expected it to use still passes. Likewise, a method that returns string satisfies a type that expects string | number, and an object with additional properties is assignable to a type that mentions fewer (see Pitfall 2). When something passes that looks suspicious, check whether it is simply assignable. satisfies does not enforce exact shapes.
Pitfall 7: tooling and version support
satisfies was added in TypeScript 4.9. Every tool in your pipeline that parses TypeScript must understand it:
- The compiler (
tsc) and editor language service must be 4.9 or newer. An older editor extension may show false errors even when the build is fine. - Transpilers that only strip types, such as Babel, esbuild and SWC, need a version that parses the syntax. Once they do, they remove it. These tools do not type-check, so you still need
tsc --noEmitin your build or CI to run the check. - Formatters and linters, such as Prettier and the TypeScript ESLint parser, need versions that recognize the keyword.
If a build fails with a syntax error pointing at the word satisfies, an outdated parser is the likely cause. The code itself is not wrong.
Pitfall 8: it cannot declare a type
satisfies applies to an expression. It cannot be placed on a declaration, a parameter, a return type, or a class member. The following does not compile:
// Syntax error: satisfies belongs after an expression, not a binding name.
// const config satisfies Options = { port: 80 };For function parameters and return values, use annotations. For a value you export and share across modules, remember that the exported type is the inferred one. That is often what you want, since consumers get the precise type. It also means a change to the literal can change your public API type without any annotation making the change visible in a review.
A summary of the limits
| Expectation | What actually happens | What to do |
|---|---|---|
| Validates parsed JSON | Passes when the value is `any`, and nothing runs at runtime | Parse as `unknown` and use a validator |
| Rejects every extra property | Only fresh literals are excess-checked, not variables or spreads | Keep the literal inside the expression |
| Later assignments use the `satisfies` type | They use the inferred type | Use an annotation for mutable variables |
| Literal types appear automatically | Only when the context contains literals | Add `as const` or a literal-union constraint |
| Arrays work with `as const` | They become readonly | Write `readonly T[]` in the constraint |
| Enforces exact shapes | Uses ordinary assignability | Test or use stricter helper types if exactness matters |
| Works in any toolchain | Needs TypeScript 4.9 or newer, and tools that parse it | Update tools and run `tsc --noEmit` |
When another tool is the better choice
Use these questions as a checklist.
- Does the value come from outside the program? Use a runtime validator.
satisfiescannot help. - Will the variable be reassigned with values that the declared type allows but the inferred type does not? Use an annotation.
- Do you need to assert something the compiler cannot verify, such as a DOM element's concrete type? That is the job of
as, with the risks described in the earlier chapter. Prefer a type guard where possible. - Do you need the exact literal values and a check that they fit a shape? Use
as const satisfies. - Do you only need to confirm a literal matches a shape, while keeping its precise members? Use
satisfies.
Exercise
A teammate writes const settings = JSON.parse(raw) satisfies Settings; and says the config is now validated. Explain why this is wrong and describe two changes that make the code safe.
Show answer
JSON.parse returns any, and any is assignable to every type, so the check always passes. The type of settings also stays any. Satisfies is erased at compile time, so nothing runs when raw is parsed. To fix it, assign the result to a variable of type unknown, then validate it with a type guard or a schema library and throw an error on failure, returning a value typed Settings only after the check passes. A second change is to keep satisfies only for literals written in the source, such as default settings.
Exercise
Given type Settings = { retries: number | undefined }; and let s = { retries: 3 } satisfies Settings;, why does s.retries = undefined; fail, and which change lets it compile while still catching a misspelled property in the initial value?
Show answer
The variable s has the inferred type { retries: number }. Satisfies checked the literal once and did not change that type, so later writes are checked against the inferred type, which excludes undefined. Declaring the variable with an annotation, let s: Settings = { retries: 3 }, allows the assignment. The annotation also applies excess property checking to the fresh literal, so a misspelled property in the initial value is still reported.
Exercise
Why does const merged = { ...base } satisfies Options; accept a base object that has a misspelled property, and how can you make the check stricter?
Show answer
Excess property checks apply only to properties written directly in a fresh object literal. Properties that arrive through a spread, or through a variable, are checked only for assignability, and extra properties are assignable. To get the stricter check, write the properties directly in the literal that carries the satisfies, or declare base itself with satisfies Options at the point where the literal is created.