Skip to content

unthrown


unthrown / P

P

The P module contains patterns for primitive types, wildcards and other pattern-matching utilities.

Interfaces

unstable_Fn

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/types/helpers.d.ts:72

Properties

PropertyTypeDefined in
inputunknownnode_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/types/helpers.d.ts:73
outputunknownnode_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/types/helpers.d.ts:74

Type Aliases

infer

ts
type infer<pattern> = InvertPattern<NoInfer<pattern>, unknown>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:67

P.infer<typeof somePattern> will return the type of the value matched by this pattern.

Read the documentation for P.infer on GitHub

Type Parameters

Type Parameter
pattern

Example

ts
const userPattern = { name: P.string }
type User = P.infer<typeof userPattern>

matcher

ts
type matcher = typeof matcher;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/internals/symbols.d.ts:15


narrow

ts
type narrow<input, pattern> = ExtractPreciseValue<input, InvertPattern<pattern, input>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:81

P.narrow<Input, Pattern> will narrow the input type to only keep the set of values that are compatible with the provided pattern type.

Read the documentation for P.narrow on GitHub

Type Parameters

Type Parameter
input
pattern

Example

ts
type Input = ['a' | 'b' | 'c', 'a' | 'b' | 'c']
const Pattern = ['a', P.union('a', 'b')] as const

type Narrowed = P.narrow<Input, typeof Pattern>
//     ^? ['a', 'a' | 'b']

Pattern

ts
type Pattern<a> = unknown extends a ? UnknownValuePattern : KnownPattern<a>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/types/Pattern.d.ts:65

Pattern<a> is the generic type for patterns matching a value of type a. A pattern can be any (nested) javascript value.

They can also be wildcards, like P._, P.string, P.number, or other matchers, like P.when(predicate), P.not(pattern), etc.

Read the documentation for P.Pattern on GitHub

Type Parameters

Type ParameterDefault type
aunknown

Example

ts
const pattern: P.Pattern<User> = { name: P.string }

unstable_Matchable

ts
type unstable_Matchable<narrowedOrFn, input, pattern> = CustomP<input, pattern, narrowedOrFn>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:42

Experimental

A Matchable is an object implementing the Matcher Protocol. It must have a [P.matcher]: P.Matcher<NarrowFn> key, which defines how this object should be matched by TS-Pattern.

This feature is unstable.

Type Parameters

Type ParameterDefault type
narrowedOrFn-
inputunknown
patternnever

Example

ts
class Some<T> implements P.unstable_Matchable {
 [P.matcher](): P.unstable_Matcher<Some<T>>
}

unstable_Matcher

ts
type unstable_Matcher<narrowedOrFn, input, pattern> = ReturnType<CustomP<input, pattern, narrowedOrFn>[matcher]>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:56

Experimental

A Matcher is an object with match function, which defines how this object should be matched by TS-Pattern.

This feature is unstable.

Type Parameters

Type ParameterDefault type
narrowedOrFn-
inputunknown
patternnever

Example

ts
class Some<T> implements P.unstable_Matchable {
 [P.matcher](): P.unstable_Matcher<Some<T>>
}

Variables

_

ts
const _: UnknownPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:256

P._ is a wildcard pattern, matching any value. It's an alias to P.any.

Read the documentation for P._ on GitHub

Example

ts
match(value)
  .with(P._, () => 'will always match')

any

ts
const any: UnknownPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:235

P.any is a wildcard pattern, matching any value.

Read the documentation for P.any on GitHub

Example

ts
match(value)
  .with(P.any, () => 'will always match')

bigint

ts
const bigint: BigIntPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:285

P.bigint is a wildcard pattern, matching any bigint.

Read the documentation for P.bigint on GitHub

Example

ts
.with(P.bigint, () => 'will match on bigints')

boolean

ts
const boolean: BooleanPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:294

P.boolean is a wildcard pattern, matching any boolean.

Read the documentation for P.boolean on GitHub

Example

ts
.with(P.boolean, () => 'will match on booleans')

matcher

ts
const matcher: unique symbol;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/internals/symbols.d.ts:15


nonNullable

ts
const nonNullable: NonNullablePattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:321

P.nonNullable is a wildcard pattern, matching everything except null or undefined.

Read the documentation for P.nonNullable on GitHub

Example

ts
.with(P.nonNullable, (x) => `${x} isn't null nor undefined`)

nullish

ts
const nullish: NullishPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:312

P.nullish is a wildcard pattern, matching null or undefined.

Read the documentation for P.nullish on GitHub

Example

ts
.with(P.nullish, (x) => `${x} is null or undefined`)

number

ts
const number: NumberPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:276

P.number is a wildcard pattern, matching any number.

Read the documentation for P.number on GitHub

Example

ts
match(value)
  .with(P.number, () => 'will match on numbers')

string

ts
const string: StringPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:266

P.string is a wildcard pattern, matching any string.

Read the documentation for P.string on GitHub

Example

ts
match(value)
  .with(P.string, () => 'will match on strings')

symbol

ts
const symbol: SymbolPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:303

P.symbol is a wildcard pattern, matching any symbol.

Read the documentation for P.symbol on GitHub

Example

ts
.with(P.symbol, () => 'will match on symbols')

unknown

ts
const unknown: UnknownPattern;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:245

P.unknown is a wildcard pattern, matching unknown value.

Read the documentation for P.unknown on GitHub

Example

ts
match(value)
  .with(P.unknown, () => 'will always match')

Functions

array()

Call Signature

ts
function array<input>(): ArrayChainable<ArrayP<input, unknown>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:110

P.array(subpattern) takes a sub pattern and returns a pattern, which matches arrays if all their elements match the sub pattern.

Read the documentation for P.array on GitHub

Type Parameters
Type Parameter
input
Returns

ArrayChainable<ArrayP<input, unknown>>

Example
ts
match(value)
  .with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')

Call Signature

ts
function array<input, pattern>(pattern): ArrayChainable<ArrayP<input, pattern>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:111

P.array(subpattern) takes a sub pattern and returns a pattern, which matches arrays if all their elements match the sub pattern.

Read the documentation for P.array on GitHub

Type Parameters
Type Parameter
input
pattern extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapArray<input>, unknown>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapArray<input>, unknown>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
Parameters
ParameterType
patternpattern
Returns

ArrayChainable<ArrayP<input, pattern>>

Example
ts
match(value)
  .with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')

instanceOf()

ts
function instanceOf<T>(classConstructor): Chainable<GuardP<unknown, InstanceType<T>>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:330

P.instanceOf(SomeClass) is a pattern matching instances of a given class.

Read the documentation for P.instanceOf on GitHub

Type Parameters

Type Parameter
T extends AnyConstructor

Parameters

ParameterType
classConstructorT

Returns

Chainable<GuardP<unknown, InstanceType<T>>>

Example

ts
.with(P.instanceOf(SomeClass), () => 'will match on SomeClass instances')

intersection()

ts
function intersection<input, patterns>(...patterns): Chainable<AndP<input, patterns>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:171

P.intersection(...patterns) returns a pattern which matches only if every patterns provided in parameter match the input.

Read the documentation for P.intersection on GitHub

Type Parameters

Type Parameter
input
patterns extends readonly [Pattern<input>, Pattern<input>]

Parameters

ParameterType
...patternspatterns

Returns

Chainable<AndP<input, patterns>>

Example

ts
match(value)
  .with(
    {
      user: P.intersection(
        { firstname: P.string },
        { lastname: P.string },
        { age: P.when(age => age > 21) }
      )
    },
    ({ user }) => 'will match { firstname: string, lastname: string, age: number } if age > 21'
  )

map()

Call Signature

ts
function map<input>(): Chainable<MapP<input, unknown, unknown>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:136

P.map(keyPattern, valuePattern) takes a subpattern to match against the key, a subpattern to match against the value and returns a pattern that matches on maps where all elements inside the map match those two subpatterns.

Read P.map documentation on GitHub

Type Parameters
Type Parameter
input
Returns

Chainable<MapP<input, unknown, unknown>>

Example
ts
match(value)
  .with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)

Call Signature

ts
function map<input, pkey, pvalue>(patternKey, patternValue): Chainable<MapP<input, pkey, pvalue>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:137

P.map(keyPattern, valuePattern) takes a subpattern to match against the key, a subpattern to match against the value and returns a pattern that matches on maps where all elements inside the map match those two subpatterns.

Read P.map documentation on GitHub

Type Parameters
Type Parameter
input
pkey extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapMapKey<input>, unknown>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapMapKey<input>, unknown>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
pvalue extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapMapValue<input>, unknown>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapMapValue<input>, unknown>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
Parameters
ParameterType
patternKeypkey
patternValuepvalue
Returns

Chainable<MapP<input, pkey, pvalue>>

Example
ts
match(value)
  .with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)

not()

ts
function not<input, pattern>(pattern): Chainable<NotP<input, pattern>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:197

P.not(pattern) returns a pattern which matches if the sub pattern doesn't match.

Read the documentation for P.not on GitHub

Type Parameters

Type Parameter
input
pattern extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<input> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | null | undefined

Parameters

ParameterType
patternpattern

Returns

Chainable<NotP<input, pattern>>

Example

ts
match<{ a: string | number }>(value)
  .with({ a: P.not(P.string) }, (x) => 'will match { a: number }'
  )

optional()

ts
function optional<input, pattern>(pattern): Chainable<OptionalP<input, pattern>, "optional">;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:92

P.optional(subpattern) takes a sub pattern and returns a pattern which matches if the key is undefined or if it is defined and the sub pattern matches its value.

Read the documentation for P.optional on GitHub

Type Parameters

Type Parameter
input
pattern extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | PatternMatcher<input> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | null | undefined

Parameters

ParameterType
patternpattern

Returns

Chainable<OptionalP<input, pattern>, "optional">

Example

ts
match(value)
  .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')

record()

Call Signature

ts
function record<input, pvalue>(patternValue): Chainable<RecordP<input, StringPattern, pvalue>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:150

P.record(keyPattern, valuePattern) takes a subpattern to match against the key, a subpattern to match against the value and returns a pattern that matches on objects where all entries match those two subpatterns.

Read P.record documentation on GitHub

Type Parameters
Type Parameter
input
pvalue extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<UnwrapRecordValue<input>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<UnwrapRecordValue<input>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
Parameters
ParameterType
patternValuepvalue
Returns

Chainable<RecordP<input, StringPattern, pvalue>>

Example
ts
match(value)
  .with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)

Call Signature

ts
function record<input, pkey, pvalue>(patternKey, patternValue?): Chainable<RecordP<input, pkey, pvalue>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:151

P.record(keyPattern, valuePattern) takes a subpattern to match against the key, a subpattern to match against the value and returns a pattern that matches on objects where all entries match those two subpatterns.

Read P.record documentation on GitHub

Type Parameters
Type Parameter
input
pkey extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapRecordKey<input>, PropertyKey>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapRecordKey<input>, PropertyKey>, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | null | undefined
pvalue extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapRecordValue<input>, unknown>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapRecordValue<input>, unknown>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
Parameters
ParameterType
patternKeypkey
patternValue?pvalue
Returns

Chainable<RecordP<input, pkey, pvalue>>

Example
ts
match(value)
  .with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)

select()

Call Signature

ts
function select(): Chainable<AnonymousSelectP, "and" | "or" | "select">;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:222

P.select() is a pattern which will always match, and will inject the selected piece of input in the handler function.

Read the documentation for P.select on GitHub

Returns

Chainable<AnonymousSelectP, "and" | "or" | "select">

Example
ts
match<{ age: number }>(value)
  .with({ age: P.select() }, (age) => 'age: number'
  )

Call Signature

ts
function select<input, patternOrKey>(patternOrKey): patternOrKey extends string ? Chainable<SelectP<patternOrKey, "and" | "or" | "select">> : Chainable<SelectP<"@ts-pattern/anonymous-select-key", input, patternOrKey>, "and" | "or" | "select">;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:223

P.select() is a pattern which will always match, and will inject the selected piece of input in the handler function.

Read the documentation for P.select on GitHub

Type Parameters
Type Parameter
input
patternOrKey extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<input> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | null | undefined
Parameters
ParameterType
patternOrKeypatternOrKey
Returns

patternOrKey extends string ? Chainable<SelectP<patternOrKey, "and" | "or" | "select">> : Chainable<SelectP<"@ts-pattern/anonymous-select-key", input, patternOrKey>, "and" | "or" | "select">

Example
ts
match<{ age: number }>(value)
  .with({ age: P.select() }, (age) => 'age: number'
  )

Call Signature

ts
function select<input, pattern, k>(key, pattern): Chainable<SelectP<k, input, pattern>, "and" | "or" | "select">;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:224

P.select() is a pattern which will always match, and will inject the selected piece of input in the handler function.

Read the documentation for P.select on GitHub

Type Parameters
Type Parameter
input
pattern extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<input> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | null | undefined
k extends string
Parameters
ParameterType
keyk
patternpattern
Returns

Chainable<SelectP<k, input, pattern>, "and" | "or" | "select">

Example
ts
match<{ age: number }>(value)
  .with({ age: P.select() }, (age) => 'age: number'
  )

set()

Call Signature

ts
function set<input>(): Chainable<SetP<input, unknown>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:122

P.set(subpattern) takes a sub pattern and returns a pattern that matches sets if all their elements match the sub pattern.

Read P.set documentation on GitHub

Type Parameters
Type Parameter
input
Returns

Chainable<SetP<input, unknown>>

Example
ts
match(value)
  .with({ users: P.set(P.string) }, () => 'will match Set<string>')

Call Signature

ts
function set<input, pattern>(pattern): Chainable<SetP<input, pattern>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:123

P.set(subpattern) takes a sub pattern and returns a pattern that matches sets if all their elements match the sub pattern.

Read P.set documentation on GitHub

Type Parameters
Type Parameter
input
pattern extends | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | Primitives | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<WithDefault<UnwrapSet<input>, unknown>> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<WithDefault<UnwrapSet<input>, unknown>, (readonly any[] | Primitives | Map<any, any> | Set<any>)>>>[k]> }
Parameters
ParameterType
patternpattern
Returns

Chainable<SetP<input, pattern>>

Example
ts
match(value)
  .with({ users: P.set(P.string) }, () => 'will match Set<string>')

shape()

ts
function shape<input, pattern>(pattern): Chainable<GuardP<input, InvertPattern<pattern, input>>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:345

P.shape(somePattern) lets you call methods like .optional(), .and, .or and .select() On structural patterns, like objects and arrays.

Read the documentation for P.shape on GitHub

Type Parameters

Type Parameter
input
pattern extends | string | number | bigint | boolean | symbol | readonly [] | readonly [unknown, unknown] | readonly [unknown, unknown] | UnknownProperties | UnknownMatcher | readonly UnknownValuePattern[] | readonly [UnknownValuePattern, UnknownValuePattern] | readonly [UnknownValuePattern, UnknownValuePattern] | PatternMatcher<input> | { readonly [k in string | number | symbol]?: Pattern<Readonly<MergeUnion<Exclude<input, readonly any[] | Primitives | Map<any, any> | Set<any>>>>[k]> } | null | undefined

Parameters

ParameterType
patternpattern

Returns

Chainable<GuardP<input, InvertPattern<pattern, input>>>

Example

ts
.with(
    {
      state: P.shape({ status: "success" }).optional().select()
    },
    (state) => 'match the success state, or undefined.'
  )

union()

ts
function union<input, patterns>(...patterns): Chainable<OrP<input, patterns>>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:185

P.union(...patterns) returns a pattern which matches if at least one of the patterns provided in parameter match the input.

Read the documentation for P.union on GitHub

Type Parameters

Type Parameter
input
patterns extends readonly [Pattern<input>, Pattern<input>]

Parameters

ParameterType
...patternspatterns

Returns

Chainable<OrP<input, patterns>>

Example

ts
match(value)
  .with(
    { type: P.union('a', 'b', 'c') },
    ({ type }) => 'will match { type: "a" | "b" | "c" }'
  )

when()

Call Signature

ts
function when<input, predicate>(predicate): GuardP<input, predicate extends (value) => value is narrowed ? narrowed : never>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:209

P.when((value) => boolean) returns a pattern which matches if the predicate returns true for the current input.

Read the documentation for P.when on GitHub

Type Parameters
Type Parameter
input
predicate extends (value) => unknown
Parameters
ParameterType
predicatepredicate
Returns

GuardP<input, predicate extends (value) => value is narrowed ? narrowed : never>

Example
ts
match<{ age: number }>(value)
  .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
  )

Call Signature

ts
function when<input, narrowed, excluded>(predicate): GuardExcludeP<input, narrowed, excluded>;

Defined in: node_modules/.pnpm/ts-pattern@5.9.0/node_modules/ts-pattern/dist/patterns.d.ts:210

P.when((value) => boolean) returns a pattern which matches if the predicate returns true for the current input.

Read the documentation for P.when on GitHub

Type Parameters
Type Parameter
input
narrowed
excluded
Parameters
ParameterType
predicate(input) => input is narrowed
Returns

GuardExcludeP<input, narrowed, excluded>

Example
ts
match<{ age: number }>(value)
  .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
  )

Released under the MIT License.