@cjser/ts-pattern 5.9.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1776 -0
  3. package/dist/errors.d.cts +8 -0
  4. package/dist/errors.d.ts +8 -0
  5. package/dist/index.cjs +2 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +5 -0
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/index.umd.js +2 -0
  12. package/dist/index.umd.js.map +1 -0
  13. package/dist/internals/helpers.d.cts +12 -0
  14. package/dist/internals/helpers.d.ts +12 -0
  15. package/dist/internals/symbols.d.cts +24 -0
  16. package/dist/internals/symbols.d.ts +24 -0
  17. package/dist/is-matching.d.cts +40 -0
  18. package/dist/is-matching.d.ts +40 -0
  19. package/dist/match.d.cts +19 -0
  20. package/dist/match.d.ts +19 -0
  21. package/dist/patterns.d.cts +345 -0
  22. package/dist/patterns.d.ts +345 -0
  23. package/dist/types/BuildMany.d.cts +22 -0
  24. package/dist/types/BuildMany.d.ts +22 -0
  25. package/dist/types/DeepExclude.d.cts +2 -0
  26. package/dist/types/DeepExclude.d.ts +2 -0
  27. package/dist/types/DistributeUnions.d.cts +111 -0
  28. package/dist/types/DistributeUnions.d.ts +111 -0
  29. package/dist/types/ExtractPreciseValue.d.cts +27 -0
  30. package/dist/types/ExtractPreciseValue.d.ts +27 -0
  31. package/dist/types/FindSelected.d.cts +78 -0
  32. package/dist/types/FindSelected.d.ts +78 -0
  33. package/dist/types/InvertPattern.d.cts +104 -0
  34. package/dist/types/InvertPattern.d.ts +104 -0
  35. package/dist/types/IsMatching.d.cts +13 -0
  36. package/dist/types/IsMatching.d.ts +13 -0
  37. package/dist/types/Match.d.cts +171 -0
  38. package/dist/types/Match.d.ts +171 -0
  39. package/dist/types/Pattern.d.cts +401 -0
  40. package/dist/types/Pattern.d.ts +401 -0
  41. package/dist/types/helpers.d.cts +80 -0
  42. package/dist/types/helpers.d.ts +80 -0
  43. package/dist/types/index.d.cts +1 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist-cjser/index.cjs +319 -0
  46. package/package.json +135 -0
@@ -0,0 +1,345 @@
1
+ /**
2
+ * The `P` module contains patterns for primitive types, wildcards and
3
+ * other pattern-matching utilities.
4
+ *
5
+ * @module
6
+ */
7
+ import * as symbols from './internals/symbols.cjs';
8
+ import { matcher } from './internals/symbols.cjs';
9
+ import { ExtractPreciseValue } from './types/ExtractPreciseValue.cjs';
10
+ import { Fn } from './types/helpers.cjs';
11
+ import { InvertPattern } from './types/InvertPattern.cjs';
12
+ import { Pattern, UnknownValuePattern, OptionalP, ArrayP, MapP, SetP, AndP, OrP, NotP, GuardP, SelectP, AnonymousSelectP, GuardExcludeP, CustomP, StringPattern, UnknownPattern, NumberPattern, BooleanPattern, BigIntPattern, NullishPattern, SymbolPattern, Chainable, ArrayChainable, NonNullablePattern, RecordP } from './types/Pattern.cjs';
13
+ export type {
14
+ /**
15
+ * `Pattern<T>` is the type of all patterns
16
+ * that can match a value of type `T`.
17
+ */
18
+ Pattern,
19
+ /**
20
+ * `unstable_Fn` can be used to created a
21
+ * a Matchable instance – a custom type that
22
+ * can be used as a pattern.
23
+ *
24
+ * @experimental This feature is unstable.
25
+ */
26
+ Fn as unstable_Fn, };
27
+ export { matcher };
28
+ /**
29
+ * A `Matchable` is an object implementing
30
+ * the Matcher Protocol. It must have a `[P.matcher]: P.Matcher<NarrowFn>`
31
+ * key, which defines how this object should be matched by TS-Pattern.
32
+ *
33
+ * @experimental This feature is unstable.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * class Some<T> implements P.unstable_Matchable {
38
+ * [P.matcher](): P.unstable_Matcher<Some<T>>
39
+ * }
40
+ * ```
41
+ */
42
+ export type unstable_Matchable<narrowedOrFn, input = unknown, pattern = never> = CustomP<input, pattern, narrowedOrFn>;
43
+ /**
44
+ * A `Matcher` is an object with `match` function, which
45
+ * defines how this object should be matched by TS-Pattern.
46
+ *
47
+ * @experimental This feature is unstable.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * class Some<T> implements P.unstable_Matchable {
52
+ * [P.matcher](): P.unstable_Matcher<Some<T>>
53
+ * }
54
+ * ```
55
+ */
56
+ export type unstable_Matcher<narrowedOrFn, input = unknown, pattern = never> = ReturnType<CustomP<input, pattern, narrowedOrFn>[matcher]>;
57
+ /**
58
+ * `P.infer<typeof somePattern>` will return the type of the value
59
+ * matched by this pattern.
60
+ *
61
+ * [Read the documentation for `P.infer` on GitHub](https://github.com/gvergnaud/ts-pattern#pinfer)
62
+ *
63
+ * @example
64
+ * const userPattern = { name: P.string }
65
+ * type User = P.infer<typeof userPattern>
66
+ */
67
+ export type infer<pattern> = InvertPattern<NoInfer<pattern>, unknown>;
68
+ /**
69
+ * `P.narrow<Input, Pattern>` will narrow the input type to only keep
70
+ * the set of values that are compatible with the provided pattern type.
71
+ *
72
+ * [Read the documentation for `P.narrow` on GitHub](https://github.com/gvergnaud/ts-pattern#pnarrow)
73
+ *
74
+ * @example
75
+ * type Input = ['a' | 'b' | 'c', 'a' | 'b' | 'c']
76
+ * const Pattern = ['a', P.union('a', 'b')] as const
77
+ *
78
+ * type Narrowed = P.narrow<Input, typeof Pattern>
79
+ * // ^? ['a', 'a' | 'b']
80
+ */
81
+ export type narrow<input, pattern> = ExtractPreciseValue<input, InvertPattern<pattern, input>>;
82
+ /**
83
+ * `P.optional(subpattern)` takes a sub pattern and returns a pattern which matches if the
84
+ * key is undefined or if it is defined and the sub pattern matches its value.
85
+ *
86
+ * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#poptional-patterns)
87
+ *
88
+ * @example
89
+ * match(value)
90
+ * .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')
91
+ */
92
+ export declare function optional<input, const pattern extends unknown extends input ? UnknownValuePattern : Pattern<input>>(pattern: pattern): Chainable<OptionalP<input, pattern>, 'optional'>;
93
+ type UnwrapArray<xs> = xs extends readonly (infer x)[] ? x : never;
94
+ type UnwrapSet<xs> = xs extends Set<infer x> ? x : never;
95
+ type UnwrapMapKey<xs> = xs extends Map<infer k, any> ? k : never;
96
+ type UnwrapMapValue<xs> = xs extends Map<any, infer v> ? v : never;
97
+ type UnwrapRecordKey<xs> = xs extends Record<infer k, any> ? k : never;
98
+ type UnwrapRecordValue<xs> = xs extends Record<any, infer v> ? v : never;
99
+ type WithDefault<a, b> = [a] extends [never] ? b : a;
100
+ /**
101
+ * `P.array(subpattern)` takes a sub pattern and returns a pattern, which matches
102
+ * arrays if all their elements match the sub pattern.
103
+ *
104
+ * [Read the documentation for `P.array` on GitHub](https://github.com/gvergnaud/ts-pattern#parray-patterns)
105
+ *
106
+ * @example
107
+ * match(value)
108
+ * .with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')
109
+ */
110
+ export declare function array<input>(): ArrayChainable<ArrayP<input, unknown>>;
111
+ export declare function array<input, const pattern extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: pattern): ArrayChainable<ArrayP<input, pattern>>;
112
+ /**
113
+ * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches
114
+ * sets if all their elements match the sub pattern.
115
+ *
116
+ * [Read `P.set` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pset-patterns)
117
+ *
118
+ * @example
119
+ * match(value)
120
+ * .with({ users: P.set(P.string) }, () => 'will match Set<string>')
121
+ */
122
+ export declare function set<input>(): Chainable<SetP<input, unknown>>;
123
+ export declare function set<input, const pattern extends Pattern<WithDefault<UnwrapSet<input>, unknown>>>(pattern: pattern): Chainable<SetP<input, pattern>>;
124
+ /**
125
+ * `P.map(keyPattern, valuePattern)` takes a subpattern to match against the
126
+ * key, a subpattern to match against the value and returns a pattern that
127
+ * matches on maps where all elements inside the map match those two
128
+ * subpatterns.
129
+ *
130
+ * [Read `P.map` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pmap-patterns)
131
+ *
132
+ * @example
133
+ * match(value)
134
+ * .with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)
135
+ */
136
+ export declare function map<input>(): Chainable<MapP<input, unknown, unknown>>;
137
+ export declare function map<input, const pkey extends Pattern<WithDefault<UnwrapMapKey<input>, unknown>>, const pvalue extends Pattern<WithDefault<UnwrapMapValue<input>, unknown>>>(patternKey: pkey, patternValue: pvalue): Chainable<MapP<input, pkey, pvalue>>;
138
+ /**
139
+ * `P.record(keyPattern, valuePattern)` takes a subpattern to match against the
140
+ * key, a subpattern to match against the value and returns a pattern that
141
+ * matches on objects where all entries match those two
142
+ * subpatterns.
143
+ *
144
+ * [Read `P.record` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#precord-patterns)
145
+ *
146
+ * @example
147
+ * match(value)
148
+ * .with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)
149
+ */
150
+ export declare function record<input, const pvalue extends Pattern<UnwrapRecordValue<input>>>(patternValue: pvalue): Chainable<RecordP<input, StringPattern, pvalue>>;
151
+ export declare function record<input, const pkey extends Pattern<WithDefault<UnwrapRecordKey<input>, PropertyKey>>, const pvalue extends Pattern<WithDefault<UnwrapRecordValue<input>, unknown>>>(patternKey: pkey, patternValue?: pvalue): Chainable<RecordP<input, pkey, pvalue>>;
152
+ /**
153
+ * `P.intersection(...patterns)` returns a pattern which matches
154
+ * only if **every** patterns provided in parameter match the input.
155
+ *
156
+ * [Read the documentation for `P.intersection` on GitHub](https://github.com/gvergnaud/ts-pattern#pintersection-patterns)
157
+ *
158
+ * @example
159
+ * match(value)
160
+ * .with(
161
+ * {
162
+ * user: P.intersection(
163
+ * { firstname: P.string },
164
+ * { lastname: P.string },
165
+ * { age: P.when(age => age > 21) }
166
+ * )
167
+ * },
168
+ * ({ user }) => 'will match { firstname: string, lastname: string, age: number } if age > 21'
169
+ * )
170
+ */
171
+ export declare function intersection<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<AndP<input, patterns>>;
172
+ /**
173
+ * `P.union(...patterns)` returns a pattern which matches
174
+ * if **at least one** of the patterns provided in parameter match the input.
175
+ *
176
+ * [Read the documentation for `P.union` on GitHub](https://github.com/gvergnaud/ts-pattern#punion-patterns)
177
+ *
178
+ * @example
179
+ * match(value)
180
+ * .with(
181
+ * { type: P.union('a', 'b', 'c') },
182
+ * ({ type }) => 'will match { type: "a" | "b" | "c" }'
183
+ * )
184
+ */
185
+ export declare function union<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<OrP<input, patterns>>;
186
+ /**
187
+ * `P.not(pattern)` returns a pattern which matches if the sub pattern
188
+ * doesn't match.
189
+ *
190
+ * [Read the documentation for `P.not` on GitHub](https://github.com/gvergnaud/ts-pattern#pnot-patterns)
191
+ *
192
+ * @example
193
+ * match<{ a: string | number }>(value)
194
+ * .with({ a: P.not(P.string) }, (x) => 'will match { a: number }'
195
+ * )
196
+ */
197
+ export declare function not<input, const pattern extends Pattern<input> | UnknownValuePattern>(pattern: pattern): Chainable<NotP<input, pattern>>;
198
+ /**
199
+ * `P.when((value) => boolean)` returns a pattern which matches
200
+ * if the predicate returns true for the current input.
201
+ *
202
+ * [Read the documentation for `P.when` on GitHub](https://github.com/gvergnaud/ts-pattern#pwhen-patterns)
203
+ *
204
+ * @example
205
+ * match<{ age: number }>(value)
206
+ * .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
207
+ * )
208
+ */
209
+ export declare function when<input, predicate extends (value: input) => unknown>(predicate: predicate): GuardP<input, predicate extends (value: any) => value is infer narrowed ? narrowed : never>;
210
+ export declare function when<input, narrowed extends input, excluded>(predicate: (input: input) => input is narrowed): GuardExcludeP<input, narrowed, excluded>;
211
+ /**
212
+ * `P.select()` is a pattern which will always match,
213
+ * and will inject the selected piece of input in the handler function.
214
+ *
215
+ * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#pselect-patterns)
216
+ *
217
+ * @example
218
+ * match<{ age: number }>(value)
219
+ * .with({ age: P.select() }, (age) => 'age: number'
220
+ * )
221
+ */
222
+ export declare function select(): Chainable<AnonymousSelectP, 'select' | 'or' | 'and'>;
223
+ export declare function select<input, const patternOrKey extends string | (unknown extends input ? UnknownValuePattern : Pattern<input>)>(patternOrKey: patternOrKey): patternOrKey extends string ? Chainable<SelectP<patternOrKey, 'select' | 'or' | 'and'>> : Chainable<SelectP<symbols.anonymousSelectKey, input, patternOrKey>, 'select' | 'or' | 'and'>;
224
+ export declare function select<input, const pattern extends unknown extends input ? UnknownValuePattern : Pattern<input>, const k extends string>(key: k, pattern: pattern): Chainable<SelectP<k, input, pattern>, 'select' | 'or' | 'and'>;
225
+ type AnyConstructor = abstract new (...args: any[]) => any;
226
+ /**
227
+ * `P.any` is a wildcard pattern, matching **any value**.
228
+ *
229
+ * [Read the documentation for `P.any` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
230
+ *
231
+ * @example
232
+ * match(value)
233
+ * .with(P.any, () => 'will always match')
234
+ */
235
+ export declare const any: UnknownPattern;
236
+ /**
237
+ * `P.unknown` is a wildcard pattern, matching **unknown value**.
238
+ *
239
+ * [Read the documentation for `P.unknown` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
240
+ *
241
+ * @example
242
+ * match(value)
243
+ * .with(P.unknown, () => 'will always match')
244
+ */
245
+ export declare const unknown: UnknownPattern;
246
+ /**
247
+ * `P._` is a wildcard pattern, matching **any value**.
248
+ * It's an alias to `P.any`.
249
+ *
250
+ * [Read the documentation for `P._` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
251
+ *
252
+ * @example
253
+ * match(value)
254
+ * .with(P._, () => 'will always match')
255
+ */
256
+ export declare const _: UnknownPattern;
257
+ /**
258
+ * `P.string` is a wildcard pattern, matching any **string**.
259
+ *
260
+ * [Read the documentation for `P.string` on GitHub](https://github.com/gvergnaud/ts-pattern#pstring-wildcard)
261
+ *
262
+ * @example
263
+ * match(value)
264
+ * .with(P.string, () => 'will match on strings')
265
+ */
266
+ export declare const string: StringPattern;
267
+ /**
268
+ * `P.number` is a wildcard pattern, matching any **number**.
269
+ *
270
+ * [Read the documentation for `P.number` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumber-wildcard)
271
+ *
272
+ * @example
273
+ * match(value)
274
+ * .with(P.number, () => 'will match on numbers')
275
+ */
276
+ export declare const number: NumberPattern;
277
+ /**
278
+ * `P.bigint` is a wildcard pattern, matching any **bigint**.
279
+ *
280
+ * [Read the documentation for `P.bigint` on GitHub](https://github.com/gvergnaud/ts-pattern#number-wildcard)
281
+ *
282
+ * @example
283
+ * .with(P.bigint, () => 'will match on bigints')
284
+ */
285
+ export declare const bigint: BigIntPattern;
286
+ /**
287
+ * `P.boolean` is a wildcard pattern, matching any **boolean**.
288
+ *
289
+ * [Read the documentation for `P.boolean` on GitHub](https://github.com/gvergnaud/ts-pattern#boolean-wildcard)
290
+ *
291
+ * @example
292
+ * .with(P.boolean, () => 'will match on booleans')
293
+ */
294
+ export declare const boolean: BooleanPattern;
295
+ /**
296
+ * `P.symbol` is a wildcard pattern, matching any **symbol**.
297
+ *
298
+ * [Read the documentation for `P.symbol` on GitHub](https://github.com/gvergnaud/ts-pattern#symbol-wildcard)
299
+ *
300
+ * @example
301
+ * .with(P.symbol, () => 'will match on symbols')
302
+ */
303
+ export declare const symbol: SymbolPattern;
304
+ /**
305
+ * `P.nullish` is a wildcard pattern, matching **null** or **undefined**.
306
+ *
307
+ * [Read the documentation for `P.nullish` on GitHub](https://github.com/gvergnaud/ts-pattern#nullish-wildcard)
308
+ *
309
+ * @example
310
+ * .with(P.nullish, (x) => `${x} is null or undefined`)
311
+ */
312
+ export declare const nullish: NullishPattern;
313
+ /**
314
+ * `P.nonNullable` is a wildcard pattern, matching everything except **null** or **undefined**.
315
+ *
316
+ * [Read the documentation for `P.nonNullable` on GitHub](https://github.com/gvergnaud/ts-pattern#nonNullable-wildcard)
317
+ *
318
+ * @example
319
+ * .with(P.nonNullable, (x) => `${x} isn't null nor undefined`)
320
+ */
321
+ export declare const nonNullable: NonNullablePattern;
322
+ /**
323
+ * `P.instanceOf(SomeClass)` is a pattern matching instances of a given class.
324
+ *
325
+ * [Read the documentation for `P.instanceOf` on GitHub](https://github.com/gvergnaud/ts-pattern#pinstanceof-patterns)
326
+ *
327
+ * @example
328
+ * .with(P.instanceOf(SomeClass), () => 'will match on SomeClass instances')
329
+ */
330
+ export declare function instanceOf<T extends AnyConstructor>(classConstructor: T): Chainable<GuardP<unknown, InstanceType<T>>>;
331
+ /**
332
+ * `P.shape(somePattern)` lets you call methods like `.optional()`, `.and`, `.or` and `.select()`
333
+ * On structural patterns, like objects and arrays.
334
+ *
335
+ * [Read the documentation for `P.shape` on GitHub](https://github.com/gvergnaud/ts-pattern#pshape-patterns)
336
+ *
337
+ * @example
338
+ * .with(
339
+ * {
340
+ * state: P.shape({ status: "success" }).optional().select()
341
+ * },
342
+ * (state) => 'match the success state, or undefined.'
343
+ * )
344
+ */
345
+ export declare function shape<input, const pattern extends Pattern<input>>(pattern: pattern): Chainable<GuardP<input, InvertPattern<pattern, input>>>;
@@ -0,0 +1,345 @@
1
+ /**
2
+ * The `P` module contains patterns for primitive types, wildcards and
3
+ * other pattern-matching utilities.
4
+ *
5
+ * @module
6
+ */
7
+ import * as symbols from './internals/symbols.js';
8
+ import { matcher } from './internals/symbols.js';
9
+ import { ExtractPreciseValue } from './types/ExtractPreciseValue.js';
10
+ import { Fn } from './types/helpers.js';
11
+ import { InvertPattern } from './types/InvertPattern.js';
12
+ import { Pattern, UnknownValuePattern, OptionalP, ArrayP, MapP, SetP, AndP, OrP, NotP, GuardP, SelectP, AnonymousSelectP, GuardExcludeP, CustomP, StringPattern, UnknownPattern, NumberPattern, BooleanPattern, BigIntPattern, NullishPattern, SymbolPattern, Chainable, ArrayChainable, NonNullablePattern, RecordP } from './types/Pattern.js';
13
+ export type {
14
+ /**
15
+ * `Pattern<T>` is the type of all patterns
16
+ * that can match a value of type `T`.
17
+ */
18
+ Pattern,
19
+ /**
20
+ * `unstable_Fn` can be used to created a
21
+ * a Matchable instance – a custom type that
22
+ * can be used as a pattern.
23
+ *
24
+ * @experimental This feature is unstable.
25
+ */
26
+ Fn as unstable_Fn, };
27
+ export { matcher };
28
+ /**
29
+ * A `Matchable` is an object implementing
30
+ * the Matcher Protocol. It must have a `[P.matcher]: P.Matcher<NarrowFn>`
31
+ * key, which defines how this object should be matched by TS-Pattern.
32
+ *
33
+ * @experimental This feature is unstable.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * class Some<T> implements P.unstable_Matchable {
38
+ * [P.matcher](): P.unstable_Matcher<Some<T>>
39
+ * }
40
+ * ```
41
+ */
42
+ export type unstable_Matchable<narrowedOrFn, input = unknown, pattern = never> = CustomP<input, pattern, narrowedOrFn>;
43
+ /**
44
+ * A `Matcher` is an object with `match` function, which
45
+ * defines how this object should be matched by TS-Pattern.
46
+ *
47
+ * @experimental This feature is unstable.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * class Some<T> implements P.unstable_Matchable {
52
+ * [P.matcher](): P.unstable_Matcher<Some<T>>
53
+ * }
54
+ * ```
55
+ */
56
+ export type unstable_Matcher<narrowedOrFn, input = unknown, pattern = never> = ReturnType<CustomP<input, pattern, narrowedOrFn>[matcher]>;
57
+ /**
58
+ * `P.infer<typeof somePattern>` will return the type of the value
59
+ * matched by this pattern.
60
+ *
61
+ * [Read the documentation for `P.infer` on GitHub](https://github.com/gvergnaud/ts-pattern#pinfer)
62
+ *
63
+ * @example
64
+ * const userPattern = { name: P.string }
65
+ * type User = P.infer<typeof userPattern>
66
+ */
67
+ export type infer<pattern> = InvertPattern<NoInfer<pattern>, unknown>;
68
+ /**
69
+ * `P.narrow<Input, Pattern>` will narrow the input type to only keep
70
+ * the set of values that are compatible with the provided pattern type.
71
+ *
72
+ * [Read the documentation for `P.narrow` on GitHub](https://github.com/gvergnaud/ts-pattern#pnarrow)
73
+ *
74
+ * @example
75
+ * type Input = ['a' | 'b' | 'c', 'a' | 'b' | 'c']
76
+ * const Pattern = ['a', P.union('a', 'b')] as const
77
+ *
78
+ * type Narrowed = P.narrow<Input, typeof Pattern>
79
+ * // ^? ['a', 'a' | 'b']
80
+ */
81
+ export type narrow<input, pattern> = ExtractPreciseValue<input, InvertPattern<pattern, input>>;
82
+ /**
83
+ * `P.optional(subpattern)` takes a sub pattern and returns a pattern which matches if the
84
+ * key is undefined or if it is defined and the sub pattern matches its value.
85
+ *
86
+ * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#poptional-patterns)
87
+ *
88
+ * @example
89
+ * match(value)
90
+ * .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: "Hello" }')
91
+ */
92
+ export declare function optional<input, const pattern extends unknown extends input ? UnknownValuePattern : Pattern<input>>(pattern: pattern): Chainable<OptionalP<input, pattern>, 'optional'>;
93
+ type UnwrapArray<xs> = xs extends readonly (infer x)[] ? x : never;
94
+ type UnwrapSet<xs> = xs extends Set<infer x> ? x : never;
95
+ type UnwrapMapKey<xs> = xs extends Map<infer k, any> ? k : never;
96
+ type UnwrapMapValue<xs> = xs extends Map<any, infer v> ? v : never;
97
+ type UnwrapRecordKey<xs> = xs extends Record<infer k, any> ? k : never;
98
+ type UnwrapRecordValue<xs> = xs extends Record<any, infer v> ? v : never;
99
+ type WithDefault<a, b> = [a] extends [never] ? b : a;
100
+ /**
101
+ * `P.array(subpattern)` takes a sub pattern and returns a pattern, which matches
102
+ * arrays if all their elements match the sub pattern.
103
+ *
104
+ * [Read the documentation for `P.array` on GitHub](https://github.com/gvergnaud/ts-pattern#parray-patterns)
105
+ *
106
+ * @example
107
+ * match(value)
108
+ * .with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')
109
+ */
110
+ export declare function array<input>(): ArrayChainable<ArrayP<input, unknown>>;
111
+ export declare function array<input, const pattern extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: pattern): ArrayChainable<ArrayP<input, pattern>>;
112
+ /**
113
+ * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches
114
+ * sets if all their elements match the sub pattern.
115
+ *
116
+ * [Read `P.set` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pset-patterns)
117
+ *
118
+ * @example
119
+ * match(value)
120
+ * .with({ users: P.set(P.string) }, () => 'will match Set<string>')
121
+ */
122
+ export declare function set<input>(): Chainable<SetP<input, unknown>>;
123
+ export declare function set<input, const pattern extends Pattern<WithDefault<UnwrapSet<input>, unknown>>>(pattern: pattern): Chainable<SetP<input, pattern>>;
124
+ /**
125
+ * `P.map(keyPattern, valuePattern)` takes a subpattern to match against the
126
+ * key, a subpattern to match against the value and returns a pattern that
127
+ * matches on maps where all elements inside the map match those two
128
+ * subpatterns.
129
+ *
130
+ * [Read `P.map` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pmap-patterns)
131
+ *
132
+ * @example
133
+ * match(value)
134
+ * .with({ users: P.map(P.map(P.string, P.number)) }, (map) => `map's type is Map<string, number>`)
135
+ */
136
+ export declare function map<input>(): Chainable<MapP<input, unknown, unknown>>;
137
+ export declare function map<input, const pkey extends Pattern<WithDefault<UnwrapMapKey<input>, unknown>>, const pvalue extends Pattern<WithDefault<UnwrapMapValue<input>, unknown>>>(patternKey: pkey, patternValue: pvalue): Chainable<MapP<input, pkey, pvalue>>;
138
+ /**
139
+ * `P.record(keyPattern, valuePattern)` takes a subpattern to match against the
140
+ * key, a subpattern to match against the value and returns a pattern that
141
+ * matches on objects where all entries match those two
142
+ * subpatterns.
143
+ *
144
+ * [Read `P.record` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#precord-patterns)
145
+ *
146
+ * @example
147
+ * match(value)
148
+ * .with({ users: P.record(P.string, P.number) }, (obj) => `object's type is Record<string, number>`)
149
+ */
150
+ export declare function record<input, const pvalue extends Pattern<UnwrapRecordValue<input>>>(patternValue: pvalue): Chainable<RecordP<input, StringPattern, pvalue>>;
151
+ export declare function record<input, const pkey extends Pattern<WithDefault<UnwrapRecordKey<input>, PropertyKey>>, const pvalue extends Pattern<WithDefault<UnwrapRecordValue<input>, unknown>>>(patternKey: pkey, patternValue?: pvalue): Chainable<RecordP<input, pkey, pvalue>>;
152
+ /**
153
+ * `P.intersection(...patterns)` returns a pattern which matches
154
+ * only if **every** patterns provided in parameter match the input.
155
+ *
156
+ * [Read the documentation for `P.intersection` on GitHub](https://github.com/gvergnaud/ts-pattern#pintersection-patterns)
157
+ *
158
+ * @example
159
+ * match(value)
160
+ * .with(
161
+ * {
162
+ * user: P.intersection(
163
+ * { firstname: P.string },
164
+ * { lastname: P.string },
165
+ * { age: P.when(age => age > 21) }
166
+ * )
167
+ * },
168
+ * ({ user }) => 'will match { firstname: string, lastname: string, age: number } if age > 21'
169
+ * )
170
+ */
171
+ export declare function intersection<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<AndP<input, patterns>>;
172
+ /**
173
+ * `P.union(...patterns)` returns a pattern which matches
174
+ * if **at least one** of the patterns provided in parameter match the input.
175
+ *
176
+ * [Read the documentation for `P.union` on GitHub](https://github.com/gvergnaud/ts-pattern#punion-patterns)
177
+ *
178
+ * @example
179
+ * match(value)
180
+ * .with(
181
+ * { type: P.union('a', 'b', 'c') },
182
+ * ({ type }) => 'will match { type: "a" | "b" | "c" }'
183
+ * )
184
+ */
185
+ export declare function union<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<OrP<input, patterns>>;
186
+ /**
187
+ * `P.not(pattern)` returns a pattern which matches if the sub pattern
188
+ * doesn't match.
189
+ *
190
+ * [Read the documentation for `P.not` on GitHub](https://github.com/gvergnaud/ts-pattern#pnot-patterns)
191
+ *
192
+ * @example
193
+ * match<{ a: string | number }>(value)
194
+ * .with({ a: P.not(P.string) }, (x) => 'will match { a: number }'
195
+ * )
196
+ */
197
+ export declare function not<input, const pattern extends Pattern<input> | UnknownValuePattern>(pattern: pattern): Chainable<NotP<input, pattern>>;
198
+ /**
199
+ * `P.when((value) => boolean)` returns a pattern which matches
200
+ * if the predicate returns true for the current input.
201
+ *
202
+ * [Read the documentation for `P.when` on GitHub](https://github.com/gvergnaud/ts-pattern#pwhen-patterns)
203
+ *
204
+ * @example
205
+ * match<{ age: number }>(value)
206
+ * .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'
207
+ * )
208
+ */
209
+ export declare function when<input, predicate extends (value: input) => unknown>(predicate: predicate): GuardP<input, predicate extends (value: any) => value is infer narrowed ? narrowed : never>;
210
+ export declare function when<input, narrowed extends input, excluded>(predicate: (input: input) => input is narrowed): GuardExcludeP<input, narrowed, excluded>;
211
+ /**
212
+ * `P.select()` is a pattern which will always match,
213
+ * and will inject the selected piece of input in the handler function.
214
+ *
215
+ * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#pselect-patterns)
216
+ *
217
+ * @example
218
+ * match<{ age: number }>(value)
219
+ * .with({ age: P.select() }, (age) => 'age: number'
220
+ * )
221
+ */
222
+ export declare function select(): Chainable<AnonymousSelectP, 'select' | 'or' | 'and'>;
223
+ export declare function select<input, const patternOrKey extends string | (unknown extends input ? UnknownValuePattern : Pattern<input>)>(patternOrKey: patternOrKey): patternOrKey extends string ? Chainable<SelectP<patternOrKey, 'select' | 'or' | 'and'>> : Chainable<SelectP<symbols.anonymousSelectKey, input, patternOrKey>, 'select' | 'or' | 'and'>;
224
+ export declare function select<input, const pattern extends unknown extends input ? UnknownValuePattern : Pattern<input>, const k extends string>(key: k, pattern: pattern): Chainable<SelectP<k, input, pattern>, 'select' | 'or' | 'and'>;
225
+ type AnyConstructor = abstract new (...args: any[]) => any;
226
+ /**
227
+ * `P.any` is a wildcard pattern, matching **any value**.
228
+ *
229
+ * [Read the documentation for `P.any` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
230
+ *
231
+ * @example
232
+ * match(value)
233
+ * .with(P.any, () => 'will always match')
234
+ */
235
+ export declare const any: UnknownPattern;
236
+ /**
237
+ * `P.unknown` is a wildcard pattern, matching **unknown value**.
238
+ *
239
+ * [Read the documentation for `P.unknown` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
240
+ *
241
+ * @example
242
+ * match(value)
243
+ * .with(P.unknown, () => 'will always match')
244
+ */
245
+ export declare const unknown: UnknownPattern;
246
+ /**
247
+ * `P._` is a wildcard pattern, matching **any value**.
248
+ * It's an alias to `P.any`.
249
+ *
250
+ * [Read the documentation for `P._` on GitHub](https://github.com/gvergnaud/ts-pattern#p_-wildcard)
251
+ *
252
+ * @example
253
+ * match(value)
254
+ * .with(P._, () => 'will always match')
255
+ */
256
+ export declare const _: UnknownPattern;
257
+ /**
258
+ * `P.string` is a wildcard pattern, matching any **string**.
259
+ *
260
+ * [Read the documentation for `P.string` on GitHub](https://github.com/gvergnaud/ts-pattern#pstring-wildcard)
261
+ *
262
+ * @example
263
+ * match(value)
264
+ * .with(P.string, () => 'will match on strings')
265
+ */
266
+ export declare const string: StringPattern;
267
+ /**
268
+ * `P.number` is a wildcard pattern, matching any **number**.
269
+ *
270
+ * [Read the documentation for `P.number` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumber-wildcard)
271
+ *
272
+ * @example
273
+ * match(value)
274
+ * .with(P.number, () => 'will match on numbers')
275
+ */
276
+ export declare const number: NumberPattern;
277
+ /**
278
+ * `P.bigint` is a wildcard pattern, matching any **bigint**.
279
+ *
280
+ * [Read the documentation for `P.bigint` on GitHub](https://github.com/gvergnaud/ts-pattern#number-wildcard)
281
+ *
282
+ * @example
283
+ * .with(P.bigint, () => 'will match on bigints')
284
+ */
285
+ export declare const bigint: BigIntPattern;
286
+ /**
287
+ * `P.boolean` is a wildcard pattern, matching any **boolean**.
288
+ *
289
+ * [Read the documentation for `P.boolean` on GitHub](https://github.com/gvergnaud/ts-pattern#boolean-wildcard)
290
+ *
291
+ * @example
292
+ * .with(P.boolean, () => 'will match on booleans')
293
+ */
294
+ export declare const boolean: BooleanPattern;
295
+ /**
296
+ * `P.symbol` is a wildcard pattern, matching any **symbol**.
297
+ *
298
+ * [Read the documentation for `P.symbol` on GitHub](https://github.com/gvergnaud/ts-pattern#symbol-wildcard)
299
+ *
300
+ * @example
301
+ * .with(P.symbol, () => 'will match on symbols')
302
+ */
303
+ export declare const symbol: SymbolPattern;
304
+ /**
305
+ * `P.nullish` is a wildcard pattern, matching **null** or **undefined**.
306
+ *
307
+ * [Read the documentation for `P.nullish` on GitHub](https://github.com/gvergnaud/ts-pattern#nullish-wildcard)
308
+ *
309
+ * @example
310
+ * .with(P.nullish, (x) => `${x} is null or undefined`)
311
+ */
312
+ export declare const nullish: NullishPattern;
313
+ /**
314
+ * `P.nonNullable` is a wildcard pattern, matching everything except **null** or **undefined**.
315
+ *
316
+ * [Read the documentation for `P.nonNullable` on GitHub](https://github.com/gvergnaud/ts-pattern#nonNullable-wildcard)
317
+ *
318
+ * @example
319
+ * .with(P.nonNullable, (x) => `${x} isn't null nor undefined`)
320
+ */
321
+ export declare const nonNullable: NonNullablePattern;
322
+ /**
323
+ * `P.instanceOf(SomeClass)` is a pattern matching instances of a given class.
324
+ *
325
+ * [Read the documentation for `P.instanceOf` on GitHub](https://github.com/gvergnaud/ts-pattern#pinstanceof-patterns)
326
+ *
327
+ * @example
328
+ * .with(P.instanceOf(SomeClass), () => 'will match on SomeClass instances')
329
+ */
330
+ export declare function instanceOf<T extends AnyConstructor>(classConstructor: T): Chainable<GuardP<unknown, InstanceType<T>>>;
331
+ /**
332
+ * `P.shape(somePattern)` lets you call methods like `.optional()`, `.and`, `.or` and `.select()`
333
+ * On structural patterns, like objects and arrays.
334
+ *
335
+ * [Read the documentation for `P.shape` on GitHub](https://github.com/gvergnaud/ts-pattern#pshape-patterns)
336
+ *
337
+ * @example
338
+ * .with(
339
+ * {
340
+ * state: P.shape({ status: "success" }).optional().select()
341
+ * },
342
+ * (state) => 'match the success state, or undefined.'
343
+ * )
344
+ */
345
+ export declare function shape<input, const pattern extends Pattern<input>>(pattern: pattern): Chainable<GuardP<input, InvertPattern<pattern, input>>>;