AZ.dev

Check an object against a type without losing its literal types

| typescript: 4.9+

Use satisfies when you want the compiler to check a value against a type but keep the narrower type it inferred.

type Config = { port: number; host: string; mode: "dev" | "prod" };

const config = { port: 3030, host: "localhost", mode: "dev" } satisfies Config;

config.mode; // "dev", not "dev" | "prod"
config.port; // 3030 in editor hovers, number after any widening context

With an annotation the check is the same but the type becomes Config, so config.mode is the full union and a switch on it has to handle "prod" too.

const config: Config = { port: 3030, host: "localhost", mode: "dev" };
config.mode; // "dev" | "prod"

When to use which

Gotchas

satisfies inference literal-types const

This entry is one card on a printable cheat sheet:

TypeScript Essentials Cheat Sheet

Was this helpful?

Related Entries

typescript-eslint --fix can break Prisma JSON column writesMake a switch fail to compile when a union case is missingMake array and record lookups admit they can be undefined