ts-pattern
Advanced tools
Comparing version 5.0.3-rc.1 to 5.0.3-rc.2
@@ -50,6 +50,19 @@ import * as symbols from './internals/symbols.js'; | ||
*/ | ||
export type infer<p extends Pattern<any>> = InvertPattern<p, unknown>; | ||
export type narrow<i, p extends Pattern<any>> = ExtractPreciseValue<i, InvertPattern<p, i>>; | ||
type Variadic<p> = p & Iterable<p>; | ||
type ArrayChainable<p, omitted extends string = never> = Variadic<p> & Omit<{ | ||
export type infer<pattern extends Pattern<any>> = InvertPattern<pattern, unknown>; | ||
/** | ||
* `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](https://github.com/gvergnaud/ts-pattern#Pnarrow) | ||
* | ||
* @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'] | ||
*/ | ||
export type narrow<input, pattern extends Pattern<any>> = ExtractPreciseValue<input, InvertPattern<pattern, input>>; | ||
type Variadic<pattern> = pattern & Iterable<pattern>; | ||
type ArrayChainable<pattern, omitted extends string = never> = Variadic<pattern> & Omit<{ | ||
/** | ||
@@ -65,3 +78,3 @@ * `.optional()` returns a pattern which matches if the | ||
*/ | ||
optional<input>(): ArrayChainable<OptionalP<input, p>, omitted | 'optional'>; | ||
optional<input>(): ArrayChainable<OptionalP<input, pattern>, omitted | 'optional'>; | ||
/** | ||
@@ -76,4 +89,4 @@ * `P.select()` will inject this property into the handler function's arguments. | ||
*/ | ||
select<input>(): ArrayChainable<SelectP<symbols.anonymousSelectKey, input, p>, omitted | 'select'>; | ||
select<input, k extends string>(key: k): ArrayChainable<SelectP<k, input, p>, omitted | 'select'>; | ||
select<input>(): ArrayChainable<SelectP<symbols.anonymousSelectKey, input, pattern>, omitted | 'select'>; | ||
select<input, k extends string>(key: k): ArrayChainable<SelectP<k, input, pattern>, omitted | 'select'>; | ||
}, omitted>; | ||
@@ -90,3 +103,3 @@ /** | ||
*/ | ||
export declare function optional<input, const p extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: p): Chainable<OptionalP<input, p>, 'optional'>; | ||
export declare function optional<input, const pattern extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: pattern): Chainable<OptionalP<input, pattern>, 'optional'>; | ||
type UnwrapArray<xs> = xs extends readonly (infer x)[] ? x : never; | ||
@@ -108,3 +121,3 @@ type UnwrapSet<xs> = xs extends Set<infer x> ? x : never; | ||
export declare function array<input>(): ArrayChainable<ArrayP<input, unknown>>; | ||
export declare function array<input, const p extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: p): ArrayChainable<ArrayP<input, p>>; | ||
export declare function array<input, const pattern extends Pattern<WithDefault<UnwrapArray<input>, unknown>>>(pattern: pattern): ArrayChainable<ArrayP<input, pattern>>; | ||
/** | ||
@@ -121,3 +134,3 @@ * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches | ||
export declare function set<input>(): Chainable<SetP<input, unknown>>; | ||
export declare function set<input, const p extends Pattern<WithDefault<UnwrapSet<input>, unknown>>>(pattern: p): Chainable<SetP<input, p>>; | ||
export declare function set<input, const pattern extends Pattern<WithDefault<UnwrapSet<input>, unknown>>>(pattern: pattern): Chainable<SetP<input, pattern>>; | ||
/** | ||
@@ -154,3 +167,3 @@ * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches | ||
*/ | ||
export declare function intersection<input, const ps extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): Chainable<AndP<input, ps>>; | ||
export declare function intersection<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<AndP<input, patterns>>; | ||
/** | ||
@@ -169,3 +182,3 @@ * `P.union(...patterns)` returns a pattern which matches | ||
*/ | ||
export declare function union<input, const ps extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): Chainable<OrP<input, ps>>; | ||
export declare function union<input, const patterns extends readonly [Pattern<input>, ...Pattern<input>[]]>(...patterns: patterns): Chainable<OrP<input, patterns>>; | ||
/** | ||
@@ -182,3 +195,3 @@ * `P.not(pattern)` returns a pattern which matches if the sub pattern | ||
*/ | ||
export declare function not<input, const p extends Pattern<input> | UnknownPattern>(pattern: p): Chainable<NotP<input, p>>; | ||
export declare function not<input, const pattern extends Pattern<input> | UnknownPattern>(pattern: pattern): Chainable<NotP<input, pattern>>; | ||
/** | ||
@@ -195,3 +208,3 @@ * `P.when((value) => boolean)` returns a pattern which matches | ||
*/ | ||
export declare function when<input, p extends (value: input) => unknown>(predicate: p): GuardP<input, p extends (value: any) => value is infer narrowed ? narrowed : never>; | ||
export declare function when<input, predicate extends (value: input) => unknown>(predicate: predicate): GuardP<input, predicate extends (value: any) => value is infer narrowed ? narrowed : never>; | ||
export declare function when<input, narrowed extends input, excluded>(predicate: (input: input) => input is narrowed): GuardExcludeP<input, narrowed, excluded>; | ||
@@ -211,3 +224,3 @@ /** | ||
export declare function select<input, const patternOrKey extends string | (unknown extends input ? UnknownPattern : Pattern<input>)>(patternOrKey: patternOrKey): patternOrKey extends string ? Chainable<SelectP<patternOrKey, 'select' | 'or' | 'and'>> : Chainable<SelectP<symbols.anonymousSelectKey, input, patternOrKey>, 'select' | 'or' | 'and'>; | ||
export declare function select<input, const p extends unknown extends input ? UnknownPattern : Pattern<input>, const k extends string>(key: k, pattern: p): Chainable<SelectP<k, input, p>, 'select' | 'or' | 'and'>; | ||
export declare function select<input, const pattern extends unknown extends input ? UnknownPattern : Pattern<input>, const k extends string>(key: k, pattern: pattern): Chainable<SelectP<k, input, pattern>, 'select' | 'or' | 'and'>; | ||
type AnyConstructor = abstract new (...args: any[]) => any; | ||
@@ -476,2 +489,2 @@ /** | ||
*/ | ||
export declare function shape<input, const p extends Pattern<input>>(pattern: p): Chainable<GuardP<input, InvertPattern<p, input>>>; | ||
export declare function shape<input, const pattern extends Pattern<input>>(pattern: pattern): Chainable<GuardP<input, InvertPattern<pattern, input>>>; |
{ | ||
"name": "ts-pattern", | ||
"version": "5.0.3-rc.1", | ||
"version": "5.0.3-rc.2", | ||
"description": " The exhaustive Pattern Matching library for TypeScript.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -1534,2 +1534,16 @@ <h1 align="center">TS-Pattern</h1> | ||
### `P.narrow` | ||
`P.narrow<Input, typeof Pattern>` will narrow the input type to only keep the set of values that are compatible with the provided pattern type. | ||
```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'] | ||
``` | ||
Note that most of the time, the `match` and `isMatching` functions perform narrowing for you, and you do not need to narrow types yourself. | ||
### `P.Pattern` | ||
@@ -1536,0 +1550,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
422597
1603
1656