Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTypeScript

satisfies vs a type annotation in TypeScript

Use satisfies to check a route table while retaining its actual keys and HTTP method types.

Annotate a route table as Record<string, Route> and TypeScript checks each HTTP method against Route. That broad annotation also allows a typo when reading a route key. Use satisfies to check the table and retain its keys.

What the annotation forgets

The annotation gives the variable the declared type. Compare what TypeScript knows about login in each version.

type Route = { path: string; method: 'GET' | 'POST' }

const annotated: Record<string, Route> = {
  login: { path: '/login', method: 'POST' },
}
const checked = {
  login: { path: '/login', method: 'POST' },
} satisfies Record<string, Route>

const a: 'POST' = annotated.login.method
const b: 'POST' = checked.login.method
annotated.hmoe.path
checked.hmoe.path

Save this as types.ts in a scratch directory and run tsc --noEmit --strict types.ts. Compare the two errors with the lines that pass.

types.ts(10,7): error TS2322: Type '"GET" | "POST"' is not assignable to type '"POST"'.
  Type '"GET"' is not assignable to type '"POST"'.
types.ts(13,9): error TS2339: Property 'hmoe' does not exist on type '{ login: { path: string; method: "POST"; }; }'.

Line 10 fails because annotated.login.method is the wide union. Line 12 compiles under --strict. Add --noUncheckedIndexedAccess and TypeScript flags that lookup as possibly undefined. Line 13 fails, and the error prints the real type of checked, with method: "POST".

Check the shape, keep the type

satisfies arrived in TypeScript 4.9. It checks assignability while keeping the expression's inferred type. The target can guide inference, as the allowed method literals do here. checked.login.method retains 'POST', while checked.login.path is still string.

I broke the object two ways to confirm the check still bites: one method set to 'PUT', and one key spelled metod.

typo.ts(5,28): error TS2322: Type '"PUT"' is not assignable to type '"GET" | "POST"'.
typo.ts(6,28): error TS2561: Object literal may only specify known properties, but 'metod' does not exist in type 'Route'. Did you mean to write 'method'?

Both errors point at the exact property, and the misspelled key is caught as an excess property.

Assertions skip the shape check

as does not validate. { path: '/login' } as Route compiled with method missing. Then login.method.toLowerCase() threw TypeError: Cannot read properties of undefined when I ran it.

I use satisfies for constants I want checked and read back with their literal types. I keep annotations where the wide type is the contract, such as function parameters. I write as only after a runtime check.