Predicate functions & satisfies operator
Write custom type guards with predicate return types and validate objects with the satisfies operator without widening.
Predicate functions & satisfies operator is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.
Intro
Goal: Build predicate functions that narrow types (e.g., value is T) and use satisfies to check shapes without widening.
- Custom guards = reusable runtime checks
satisfies= compile-time shape check, preserves literals
Predicate basics
A predicate function returns x is Type. After the check, the variable is narrowed in that scope.
function isNumber(x: unknown): x is number {
return typeof x === "number"
}
function demo(a: unknown) {
if (isNumber(a)) {
// inside: a is number
return a.toFixed(2)
}
return "not a number"
}Shape guard
Guard checks the shape of the objects; if it passes, it is collapsed as User and can be used safely.
type User = { id: number; name: string }
function isUser(v: unknown): v is User {
return typeof v === "object" && v !== null &&
"id" in v && "name" in v &&
typeof (v as any).id === "number" &&
typeof (v as any).name === "string"
}
function printUser(u: unknown) {
if (isUser(u)) {
console.log(u.name.toUpperCase())
} else {
console.log("invalid user")
}
}Across boundaries
Guards ensure safe transport across function boundaries: unknown outside, controlled User inside.
function fetchAndPrint(json: string) {
const value: unknown = JSON.parse(json)
if (isUser(value)) {
// value narrowed to User
console.log(`User #${value.id}: ${value.name}`)
} else {
console.log("Bad payload")
}
}satisfies (config)
satisfies checks the expression for conformity with Confige, but preserves the literal type of the variable.
type Config = { mode: "dev" | "prod"; retries: number }
const cfg = {
mode: "prod",
retries: 3,
// timeout: 1000, // Error with satisfies if not in type
} satisfies Config
// cfg.mode is still the literal "prod" (not widened to string)
// and adding unknown keys raises an error at compile-timesatisfies (arrays)
In arrays, element shapes are validated with as const satisfies; literal values are preserved, and auto-completion is enhanced.
type Route = { path: `/${string}`; method: "GET" | "POST" }
const routes = [
{ path: "/", method: "GET" },
{ path: "/login", method: "POST" }
] as const satisfies ReadonlyArray<Route>
// each element is checked against Route, literals preserved for DXsatisfies check
Quick check: What does satisfies actually do?
Recap
Recap: Predicates (x is T) contracts; satisfies confirms form and preserves literal types. Use both together.
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “Predicate functions & satisfies operator” lesson free?
Yes — the full text of “Predicate functions & satisfies operator” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.
What will I learn in “Predicate functions & satisfies operator”?
Write custom type guards with predicate return types and validate objects with the satisfies operator without widening. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Predicate functions & satisfies operator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Exhaustive switches & never checks
- Predicate functions & satisfies operator
- Refining unions across function boundaries