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
Type Aliases
infer
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
const userPattern = { name: P.string }
type User = P.infer<typeof userPattern>matcher
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
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
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
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 Parameter | Default type |
|---|---|
a | unknown |
Example
const pattern: P.Pattern<User> = { name: P.string }unstable_Matchable
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 Parameter | Default type |
|---|---|
narrowedOrFn | - |
input | unknown |
pattern | never |
Example
class Some<T> implements P.unstable_Matchable {
[P.matcher](): P.unstable_Matcher<Some<T>>
}unstable_Matcher
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 Parameter | Default type |
|---|---|
narrowedOrFn | - |
input | unknown |
pattern | never |
Example
class Some<T> implements P.unstable_Matchable {
[P.matcher](): P.unstable_Matcher<Some<T>>
}Variables
_
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
match(value)
.with(P._, () => 'will always match')any
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
match(value)
.with(P.any, () => 'will always match')bigint
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
.with(P.bigint, () => 'will match on bigints')boolean
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
.with(P.boolean, () => 'will match on booleans')matcher
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
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
.with(P.nonNullable, (x) => `${x} isn't null nor undefined`)nullish
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
.with(P.nullish, (x) => `${x} is null or undefined`)number
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
match(value)
.with(P.number, () => 'will match on numbers')string
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
match(value)
.with(P.string, () => 'will match on strings')symbol
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
.with(P.symbol, () => 'will match on symbols')unknown
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
match(value)
.with(P.unknown, () => 'will always match')Functions
array()
Call Signature
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
match(value)
.with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')Call Signature
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
| Parameter | Type |
|---|---|
pattern | pattern |
Returns
ArrayChainable<ArrayP<input, pattern>>
Example
match(value)
.with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')instanceOf()
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
| Parameter | Type |
|---|---|
classConstructor | T |
Returns
Chainable<GuardP<unknown, InstanceType<T>>>
Example
.with(P.instanceOf(SomeClass), () => 'will match on SomeClass instances')intersection()
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
| Parameter | Type |
|---|---|
...patterns | patterns |
Returns
Chainable<AndP<input, patterns>>
Example
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
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
match(value)
.with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)Call Signature
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
| Parameter | Type |
|---|---|
patternKey | pkey |
patternValue | pvalue |
Returns
Chainable<MapP<input, pkey, pvalue>>
Example
match(value)
.with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)not()
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
| Parameter | Type |
|---|---|
pattern | pattern |
Returns
Chainable<NotP<input, pattern>>
Example
match<{ a: string | number }>(value)
.with({ a: P.not(P.string) }, (x) => 'will match { a: number }'
)optional()
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
| Parameter | Type |
|---|---|
pattern | pattern |
Returns
Chainable<OptionalP<input, pattern>, "optional">
Example
match(value)
.with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')record()
Call Signature
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
| Parameter | Type |
|---|---|
patternValue | pvalue |
Returns
Chainable<RecordP<input, StringPattern, pvalue>>
Example
match(value)
.with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)Call Signature
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
| Parameter | Type |
|---|---|
patternKey | pkey |
patternValue? | pvalue |
Returns
Chainable<RecordP<input, pkey, pvalue>>
Example
match(value)
.with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)select()
Call Signature
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
match<{ age: number }>(value)
.with({ age: P.select() }, (age) => 'age: number'
)Call Signature
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
| Parameter | Type |
|---|---|
patternOrKey | patternOrKey |
Returns
patternOrKey extends string ? Chainable<SelectP<patternOrKey, "and" | "or" | "select">> : Chainable<SelectP<"@ts-pattern/anonymous-select-key", input, patternOrKey>, "and" | "or" | "select">
Example
match<{ age: number }>(value)
.with({ age: P.select() }, (age) => 'age: number'
)Call Signature
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
| Parameter | Type |
|---|---|
key | k |
pattern | pattern |
Returns
Chainable<SelectP<k, input, pattern>, "and" | "or" | "select">
Example
match<{ age: number }>(value)
.with({ age: P.select() }, (age) => 'age: number'
)set()
Call Signature
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
match(value)
.with({ users: P.set(P.string) }, () => 'will match Set<string>')Call Signature
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
| Parameter | Type |
|---|---|
pattern | pattern |
Returns
Chainable<SetP<input, pattern>>
Example
match(value)
.with({ users: P.set(P.string) }, () => 'will match Set<string>')shape()
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
| Parameter | Type |
|---|---|
pattern | pattern |
Returns
Chainable<GuardP<input, InvertPattern<pattern, input>>>
Example
.with(
{
state: P.shape({ status: "success" }).optional().select()
},
(state) => 'match the success state, or undefined.'
)union()
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
| Parameter | Type |
|---|---|
...patterns | patterns |
Returns
Chainable<OrP<input, patterns>>
Example
match(value)
.with(
{ type: P.union('a', 'b', 'c') },
({ type }) => 'will match { type: "a" | "b" | "c" }'
)when()
Call Signature
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
| Parameter | Type |
|---|---|
predicate | predicate |
Returns
GuardP<input, predicate extends (value) => value is narrowed ? narrowed : never>
Example
match<{ age: number }>(value)
.with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
)Call Signature
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
| Parameter | Type |
|---|---|
predicate | (input) => input is narrowed |
Returns
GuardExcludeP<input, narrowed, excluded>
Example
match<{ age: number }>(value)
.with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
)