Skip to Content
Objects

Last Updated: 3/6/2026


Object Patterns

Patterns can be objects containing sub-patterns. An object pattern will match if and only if the input value is an object, contains all properties the pattern defines and each property matches the corresponding sub-pattern.

import { match } from 'ts-pattern'; type Input = | { type: 'user'; name: string } | { type: 'image'; src: string } | { type: 'video'; seconds: number }; let input: Input = { type: 'user', name: 'Gabriel' }; const output = match(input) .with({ type: 'image' }, () => 'image') .with({ type: 'video', seconds: 10 }, () => 'video of 10 seconds.') .with({ type: 'user' }, ({ name }) => `user of name: ${name}`) .otherwise(() => 'something else'); console.log(output); // => 'user of name: Gabriel'