Socket
Socket
Sign inDemoInstall

ts-pattern

Package Overview
Dependencies
Maintainers
1
Versions
151
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-pattern - npm Package Compare versions

Comparing version 4.0.1-rc.5 to 4.0.1-rc.6

3

lib/match.d.ts

@@ -0,1 +1,2 @@

import { Pattern } from './types/Pattern';
import { Match } from './types/Match';

@@ -10,2 +11,2 @@ /**

*/
export declare const match: <i, o = "@ts-pattern/unset">(value: i) => Match<i, o, [], never>;
export declare const match: <i, o = "@ts-pattern/unset">(value: i) => Match<i, o, [], never, Pattern<i>>;

@@ -14,3 +14,3 @@ import * as symbols from './internals/symbols';

export declare function optional<input, p extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: p): OptionalP<input, p>;
declare type Elem<xs> = xs extends Array<infer x> ? x : unknown;
declare type Elem<xs> = xs extends Array<infer x> ? x : never;
/**

@@ -66,3 +66,3 @@ * ### Array pattern

*/
export declare function not<input, p extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: p): NotP<input, p>;
export declare function not<input, p extends unknown extends input ? UnknownPattern : Pattern<input> | undefined>(pattern: p): NotP<input, p>;
/**

@@ -69,0 +69,0 @@ * ### When pattern

@@ -20,7 +20,15 @@ import type * as symbols from '../internals/symbols';

declare type ReduceFindSelectionUnion<i, ps extends any[], output = never> = ps extends [infer head, ...infer tail] ? ReduceFindSelectionUnion<i, tail, output | FindSelectionUnion<i, head>> : output;
export declare type FindSelectionUnion<i, p, path extends any[] = []> = IsAny<i> extends true ? never : p extends Matchable<any, infer pattern, infer matcherType, infer sel> ? matcherType extends 'select' ? sel extends Some<infer k> ? {
[kk in k]: [i, path];
} | FindSelectionUnion<i, pattern, path> : never : matcherType extends 'array' ? i extends (infer ii)[] ? MapList<FindSelectionUnion<ii, pattern>> : never : matcherType extends 'optional' ? MapOptional<FindSelectionUnion<i, pattern>> : matcherType extends 'or' ? MapOptional<ReduceFindSelectionUnion<i, Cast<pattern, any[]>>> : matcherType extends 'and' ? ReduceFindSelectionUnion<i, Cast<pattern, any[]>> : sel extends Some<infer k> ? {
[kk in k]: [i, path];
} : never : p extends readonly [any, ...any[]] ? i extends readonly [any, ...any[]] ? {
export declare type FindSelectionUnion<i, p, path extends any[] = []> = IsAny<i> extends true ? never : p extends Matchable<any, infer pattern, infer matcherType, infer sel> ? {
select: sel extends Some<infer k> ? {
[kk in k]: [i, path];
} | FindSelectionUnion<i, pattern, path> : never;
array: i extends (infer ii)[] ? MapList<FindSelectionUnion<ii, pattern>> : never;
optional: MapOptional<FindSelectionUnion<i, pattern>>;
or: MapOptional<ReduceFindSelectionUnion<i, Cast<pattern, any[]>>>;
and: ReduceFindSelectionUnion<i, Cast<pattern, any[]>>;
not: never;
default: sel extends Some<infer k> ? {
[kk in k]: [i, path];
} : never;
}[matcherType] : p extends readonly (infer pp)[] ? i extends readonly (infer ii)[] ? p extends readonly [any, ...any[]] ? i extends readonly [any, ...any[]] ? {
[k in TupleKeys & keyof i & keyof p]: FindSelectionUnion<i[k], p[k], [

@@ -30,3 +38,3 @@ ...path,

]>;
}[TupleKeys & keyof i & keyof p] : i extends readonly (infer ii)[] ? FindSelectionUnion<ii, p[number], [...path, 0]> : never : p extends readonly (infer pp)[] ? i extends readonly (infer ii)[] ? FindSelectionUnion<ii, pp, [...path, 0]> : never : p extends object ? i extends object ? {
}[TupleKeys & keyof i & keyof p] : FindSelectionUnion<ii, p[number], [...path, 0]> : FindSelectionUnion<ii, pp, [...path, 0]> : never : p extends object ? i extends object ? {
[k in keyof p]: k extends keyof i ? FindSelectionUnion<i[k], p[k], [...path, k]> : never;

@@ -33,0 +41,0 @@ }[keyof p] : never : never;

@@ -41,7 +41,4 @@ export declare type ValueOf<a> = a extends any[] ? a[number] : a[keyof a];

} & unknown;
export declare type PartitionKeys<a> = keyof Compute<a> extends infer shared ? {
shared: shared;
others: ValueOf<{
[k in keyof a]: k extends shared ? never : k;
}>;
export declare type IntersectObjects<a> = (a extends any ? keyof a : never) extends infer allKeys ? {
[k in Cast<allKeys, PropertyKey>]: a extends any ? k extends keyof a ? a[k] : never : never;
} : never;

@@ -48,0 +45,0 @@ export declare type HasObjects<a> = true extends (a extends object ? IsPlainObject<a> : false) ? true : false;

import { DeepExclude } from './DeepExclude';
import { IsPlainObject, Primitives, IsLiteral, ValueOf, Compute, Cast } from './helpers';
import { IsPlainObject, Primitives, IsLiteral, ValueOf, Compute, Cast, Equal } from './helpers';
import type { Matchable, ToExclude } from './Pattern';

@@ -17,3 +17,11 @@ declare type OptionalKeys<p> = ValueOf<{

*/
export declare type InvertPattern<p> = p extends Matchable<infer input, infer narrowed, infer matcherType, any> ? matcherType extends 'not' ? ToExclude<InvertPattern<narrowed>> : matcherType extends 'select' ? InvertPattern<narrowed> : matcherType extends 'array' ? InvertPattern<narrowed>[] : matcherType extends 'optional' ? InvertPattern<narrowed> | undefined : matcherType extends 'and' ? ReduceIntersection<Cast<narrowed, any[]>> : matcherType extends 'or' ? ReduceUnion<Cast<narrowed, any[]>> : [narrowed] extends [never] ? input : narrowed : p extends Primitives ? p : p extends readonly (infer pp)[] ? p extends readonly [infer p1, infer p2, infer p3, infer p4, infer p5] ? [
export declare type InvertPattern<p> = p extends Matchable<infer input, infer narrowed, infer matcherType, any> ? {
not: ToExclude<InvertPattern<narrowed>>;
select: InvertPattern<narrowed>;
array: InvertPattern<narrowed>[];
optional: InvertPattern<narrowed> | undefined;
and: ReduceIntersection<Cast<narrowed, any[]>>;
or: ReduceUnion<Cast<narrowed, any[]>>;
default: [narrowed] extends [never] ? input : narrowed;
}[matcherType] : p extends Primitives ? p : p extends readonly (infer pp)[] ? p extends readonly [infer p1, infer p2, infer p3, infer p4, infer p5] ? [
InvertPattern<p1>,

@@ -36,6 +44,15 @@ InvertPattern<p2>,

export declare type ReduceUnionForExclude<tuple extends any[], i, output = never> = tuple extends readonly [infer p, ...infer tail] ? ReduceUnionForExclude<tail, i, output | InvertPatternForExclude<p, i, never>> : output;
declare type ExcludeIfExists<a, b> = [b] extends [never] ? never : DeepExclude<a, b> extends infer excluded ? Equal<a, excluded> extends true ? unknown : excluded : never;
/**
* ### InvertPatternForExclude
*/
export declare type InvertPatternForExclude<p, i, empty = never> = p extends Matchable<any, infer narrowed, infer matcherType, any, infer excluded> ? matcherType extends 'not' ? InvertPatternForExclude<narrowed, i, unknown> extends infer inv ? [inv] extends [never] ? empty : DeepExclude<i, inv> : empty : matcherType extends 'select' ? InvertPatternForExclude<narrowed, i, empty> : matcherType extends 'array' ? i extends readonly (infer ii)[] ? InvertPatternForExclude<narrowed, ii, empty>[] : empty : matcherType extends 'optional' ? InvertPatternForExclude<narrowed, i, empty> | undefined : matcherType extends 'and' ? ReduceIntersectionForExclude<Cast<narrowed, any[]>, i> : matcherType extends 'or' ? ReduceUnionForExclude<Cast<narrowed, any[]>, i> : excluded : p extends Primitives ? IsLiteral<p> extends true ? p : IsLiteral<i> extends true ? p : empty : p extends readonly (infer pp)[] ? i extends readonly (infer ii)[] ? p extends readonly [infer p1, infer p2, infer p3, infer p4, infer p5] ? i extends readonly [infer i1, infer i2, infer i3, infer i4, infer i5] ? readonly [
export declare type InvertPatternForExclude<p, i, empty = never> = p extends Matchable<infer matchableInput, infer subpattern, infer matcherType, any, infer excluded> ? {
select: InvertPatternForExclude<subpattern, i, empty>;
array: i extends readonly (infer ii)[] ? InvertPatternForExclude<subpattern, ii, empty>[] : empty;
optional: InvertPatternForExclude<subpattern, i, empty> | undefined;
and: ReduceIntersectionForExclude<Cast<subpattern, any[]>, i>;
or: ReduceUnionForExclude<Cast<subpattern, any[]>, i>;
not: ExcludeIfExists<unknown extends matchableInput ? i : matchableInput, InvertPatternForExclude<subpattern, i>>;
default: excluded;
}[matcherType] : p extends Primitives ? IsLiteral<p> extends true ? p : IsLiteral<i> extends true ? p : empty : p extends readonly (infer pp)[] ? i extends readonly (infer ii)[] ? p extends readonly [infer p1, infer p2, infer p3, infer p4, infer p5] ? i extends readonly [infer i1, infer i2, infer i3, infer i4, infer i5] ? readonly [
InvertPatternForExclude<p1, i1, empty>,

@@ -42,0 +59,0 @@ InvertPatternForExclude<p2, i2, empty>,

@@ -17,3 +17,3 @@ import type { Pattern, Matchable } from './Pattern';

*/
export declare type Match<i, o, patternValueTuples extends [any, any][] = [], inferredOutput = never> = {
export declare type Match<i, o, patternValueTuples extends [any, any][] = [], inferredOutput = never, inputPattern = Pattern<i>> = {
/**

@@ -25,8 +25,34 @@ * #### Match.with

**/
with<p extends Pattern<i>, c, value extends MatchedValue<i, InvertPattern<p>>>(pattern: p, handler: (selections: FindSelected<value, p>, value: value) => PickReturnValue<o, c>): Match<i, o, [...patternValueTuples, [p, value]], Union<inferredOutput, c>>;
with<pat extends Pattern<i>, pred extends (value: MatchedValue<i, InvertPattern<pat>>) => unknown, c, value extends GuardValue<pred>>(pattern: pat, predicate: pred, handler: (selections: FindSelected<value, pat>, value: value) => PickReturnValue<o, c>): Match<i, o, pred extends (value: any) => value is infer narrowed ? [...patternValueTuples, [Matchable<unknown, narrowed>, value]] : patternValueTuples, Union<inferredOutput, c>>;
with<ps extends [Pattern<i>, ...Pattern<i>[]], c, p extends ps[number], value extends p extends any ? MatchedValue<i, InvertPattern<p>> : never>(...args: [...patterns: ps, handler: (value: value) => PickReturnValue<o, c>]): Match<i, o, [
with<p extends inputPattern, c, value extends MatchedValue<i, InvertPattern<p>>>(pattern: p, handler: (selections: FindSelected<value, p>, value: value) => PickReturnValue<o, c>): Match<i, o, [
...patternValueTuples,
[p, value]
], Union<inferredOutput, c>, inputPattern>;
with<pat extends inputPattern, pred extends (value: MatchedValue<i, InvertPattern<pat>>) => unknown, c, value extends GuardValue<pred>>(pattern: pat, predicate: pred, handler: (selections: FindSelected<value, pat>, value: value) => PickReturnValue<o, c>): Match<i, o, pred extends (value: any) => value is infer narrowed ? [...patternValueTuples, [Matchable<unknown, narrowed>, value]] : patternValueTuples, Union<inferredOutput, c>, inputPattern>;
with<p1 extends inputPattern, p2 extends inputPattern, c, p extends p1 | p2, value extends p extends any ? MatchedValue<i, InvertPattern<p>> : never>(p1: p1, p2: p2, handler: (value: value) => PickReturnValue<o, c>): Match<i, o, [
...patternValueTuples,
[p1, value],
[p2, value]
], Union<inferredOutput, c>, inputPattern>;
with<p1 extends inputPattern, p2 extends inputPattern, p3 extends inputPattern, ps extends inputPattern[], c, p extends p1 | p2 | p3 | ps[number], value extends p extends any ? MatchedValue<i, InvertPattern<p>> : never>(...args: [
p1: p1,
p2: p2,
p3: p3,
...patterns: ps,
handler: (value: value) => PickReturnValue<o, c>
]): Match<i, o, [
...patternValueTuples,
[
p1,
value
],
[
p2,
value
],
[
p3,
value
],
...MakeTuples<ps, value>
], Union<inferredOutput, c>>;
], Union<inferredOutput, c>, inputPattern>;
/**

@@ -38,3 +64,3 @@ * #### Match.when

**/
when<pred extends (value: i) => unknown, c, value extends GuardValue<pred>>(predicate: pred, handler: (value: value) => PickReturnValue<o, c>): Match<i, o, pred extends (value: any) => value is infer narrowed ? [...patternValueTuples, [Matchable<unknown, narrowed>, value]] : patternValueTuples, Union<inferredOutput, c>>;
when<pred extends (value: i) => unknown, c, value extends GuardValue<pred>>(predicate: pred, handler: (value: value) => PickReturnValue<o, c>): Match<i, o, pred extends (value: any) => value is infer narrowed ? [...patternValueTuples, [Matchable<unknown, narrowed>, value]] : patternValueTuples, Union<inferredOutput, c>, inputPattern>;
/**

@@ -41,0 +67,0 @@ * #### Match.otherwise

import type * as symbols from '../internals/symbols';
import { Primitives, Cast, IsUnion, HasObjects, PartitionObjects, PartitionKeys } from './helpers';
import { Primitives, IsUnion, HasObjects, PartitionObjects, IntersectObjects } from './helpers';
import { None, Some, SelectionType } from './FindSelected';

@@ -37,7 +37,4 @@ export declare type MatcherType = 'not' | 'optional' | 'or' | 'and' | 'array' | 'select' | 'default';

others: infer othersValues;
} ? (PartitionKeys<objects> extends {
shared: infer sharedKeys;
others: infer otherKeys;
} ? {
readonly [k in Cast<sharedKeys | otherKeys, string>]?: k extends sharedKeys ? Pattern<objects[Cast<k, keyof objects>]> : objects extends any ? k extends keyof objects ? Pattern<objects[k]> : never : never;
} ? (IntersectObjects<objects> extends infer intersectedObjects ? {
readonly [k in keyof intersectedObjects]?: Pattern<Exclude<intersectedObjects[k], undefined>>;
} : never) | ([othersValues] extends [never] ? never : othersValues extends any ? StructuralPattern<othersValues> : never) : never : a extends Primitives ? a : unknown extends a ? UnknownPattern : a extends readonly (infer i)[] ? a extends readonly [infer a1, infer a2, infer a3, infer a4, infer a5] ? readonly [

@@ -56,3 +53,3 @@ Pattern<a1>,

] : a extends Map<infer k, infer v> ? Map<k, Pattern<v>> : a extends Set<infer v> ? Set<Pattern<v>> : a extends object ? {
readonly [k in keyof a]?: Pattern<a[k]>;
readonly [k in keyof a]?: Pattern<Exclude<a[k], undefined>>;
} : a;

@@ -59,0 +56,0 @@ /**

{
"name": "ts-pattern",
"version": "4.0.1-rc.5",
"version": "4.0.1-rc.6",
"description": " The exhaustive Pattern Matching library for TypeScript.",

@@ -48,3 +48,3 @@ "main": "lib/index.js",

"jest": "^27.4.5",
"prettier": "^2.2.1",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",

@@ -51,0 +51,0 @@ "ts-jest": "^27.1.2",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc