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.3 to 4.0.1-rc.4

35

lib/patterns.d.ts

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

*/
export declare const optional: <input, p extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: p) => OptionalP<input, p>;
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;

@@ -24,3 +24,3 @@ /**

*/
export declare const array: <input, p extends unknown extends input ? UnknownPattern : Pattern<Elem<input>>>(pattern: p) => ArrayP<input, p>;
export declare function array<input, p extends unknown extends input ? UnknownPattern : Pattern<Elem<input>>>(pattern: p): ArrayP<input, p>;
/**

@@ -43,3 +43,3 @@ * ### Intersection pattern

*/
export declare const intersection: <input, ps extends unknown extends input ? [UnknownPattern, ...UnknownPattern[]] : [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps) => AndP<input, ps>;
export declare function intersection<input, ps extends unknown extends input ? [UnknownPattern, ...UnknownPattern[]] : [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): AndP<input, ps>;
/**

@@ -58,3 +58,3 @@ * ### Union pattern

*/
export declare const union: <input, ps extends unknown extends input ? [UnknownPattern, ...UnknownPattern[]] : [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps) => OrP<input, ps>;
export declare function union<input, ps extends unknown extends input ? [UnknownPattern, ...UnknownPattern[]] : [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): OrP<input, ps>;
/**

@@ -69,3 +69,3 @@ * ### Not pattern

*/
export declare const 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>>(pattern: p): NotP<input, p>;
/**

@@ -80,3 +80,3 @@ * ### When pattern

*/
export declare const when: <input, narrowed extends input = never>(predicate: GuardFunction<input, narrowed>) => GuardP<input, narrowed>;
export declare function when<input, narrowed extends input = never>(predicate: GuardFunction<input, narrowed>): GuardP<input, narrowed>;
/**

@@ -92,4 +92,3 @@ * ### Select pattern

export declare function select(): SelectP<symbols.anonymousSelectKey>;
export declare function select<k extends string>(key: k): SelectP<k>;
export declare function select<input, p extends unknown extends input ? UnknownPattern : Pattern<input>>(pattern: p): SelectP<symbols.anonymousSelectKey, input, p>;
export declare function select<input, patternOrKey extends string | (unknown extends input ? UnknownPattern : Pattern<input>)>(patternOrKey: patternOrKey): patternOrKey extends string ? SelectP<patternOrKey> : SelectP<symbols.anonymousSelectKey, input, patternOrKey>;
export declare function select<input, p extends unknown extends input ? UnknownPattern : Pattern<input>, k extends string>(key: k, pattern: p): SelectP<k, input, p>;

@@ -155,3 +154,3 @@ declare type AnyConstructor = new (...args: any[]) => any;

*/
export declare const instanceOf: <T extends AnyConstructor>(classConstructor: T) => GuardP<unknown, InstanceType<T>>;
export declare function instanceOf<T extends AnyConstructor>(classConstructor: T): GuardP<unknown, InstanceType<T>>;
/**

@@ -166,2 +165,20 @@ * ### infer

export declare type infer<p extends Pattern<any>> = InvertPattern<p>;
/**
* ### typed
* `P.typed<SomeType>()` is a way to set the input type this
* pattern should match.
*
* It returns all utility functions to create patterns,
* Like `array`, `union`, `intersection`, etc.
*/
export declare function typed<input>(): {
array<p extends Pattern<Elem<input>>>(pattern: p): ArrayP<input, p>;
optional<p extends Pattern<input>>(pattern: p): OptionalP<input, p>;
intersection<ps extends [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): AndP<input, ps>;
union<ps extends [Pattern<input>, ...Pattern<input>[]]>(...patterns: ps): OrP<input, ps>;
not<p extends Pattern<input>>(pattern: p): NotP<input, p>;
when<narrowed extends input = never>(predicate: GuardFunction<input, narrowed>): GuardP<input, narrowed>;
select<pattern extends Pattern<input>>(pattern: pattern): SelectP<symbols.anonymousSelectKey, input, pattern>;
select<p extends Pattern<input>, k extends string>(key: k, pattern: p): SelectP<k, input, p>;
};
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.instanceOf = exports.nullish = exports.symbol = exports.bigint = exports.boolean = exports.number = exports.string = exports.__ = exports.select = exports.when = exports.not = exports.union = exports.intersection = exports.array = exports.optional = void 0;
exports.typed = exports.instanceOf = exports.nullish = exports.symbol = exports.bigint = exports.boolean = exports.number = exports.string = exports.__ = exports.select = exports.when = exports.not = exports.union = exports.intersection = exports.array = exports.optional = void 0;
const helpers_1 = require("./internals/helpers");

@@ -14,3 +14,3 @@ const symbols = require("./internals/symbols");

*/
const optional = (pattern) => {
function optional(pattern) {
return {

@@ -36,3 +36,3 @@ [symbols.matcher]() {

};
};
}
exports.optional = optional;

@@ -47,3 +47,3 @@ /**

*/
const array = (pattern) => {
function array(pattern) {
return {

@@ -66,3 +66,3 @@ [symbols.matcher]() {

};
};
}
exports.array = array;

@@ -86,16 +86,18 @@ /**

*/
const intersection = (...patterns) => ({
[symbols.matcher]: () => ({
match: (value) => {
let selections = {};
const selector = (key, value) => {
selections[key] = value;
};
const matched = patterns.every((p) => (0, helpers_1.matchPattern)(p, value, selector));
return { matched, selections };
},
getSelectionKeys: () => patterns.reduce((acc, p) => acc.concat((0, helpers_1.getSelectionKeys)(p)), []),
matcherType: 'and',
}),
});
function intersection(...patterns) {
return {
[symbols.matcher]: () => ({
match: (value) => {
let selections = {};
const selector = (key, value) => {
selections[key] = value;
};
const matched = patterns.every((p) => (0, helpers_1.matchPattern)(p, value, selector));
return { matched, selections };
},
getSelectionKeys: () => (0, helpers_1.flatMap)(patterns, helpers_1.getSelectionKeys),
matcherType: 'and',
}),
};
}
exports.intersection = intersection;

@@ -115,17 +117,19 @@ /**

*/
const union = (...patterns) => ({
[symbols.matcher]: () => ({
match: (value) => {
let selections = {};
const selector = (key, value) => {
selections[key] = value;
};
(0, helpers_1.flatMap)(patterns, helpers_1.getSelectionKeys).forEach((key) => selector(key, undefined));
const matched = patterns.some((p) => (0, helpers_1.matchPattern)(p, value, selector));
return { matched, selections };
},
getSelectionKeys: () => patterns.reduce((acc, p) => acc.concat((0, helpers_1.getSelectionKeys)(p)), []),
matcherType: 'or',
}),
});
function union(...patterns) {
return {
[symbols.matcher]: () => ({
match: (value) => {
let selections = {};
const selector = (key, value) => {
selections[key] = value;
};
(0, helpers_1.flatMap)(patterns, helpers_1.getSelectionKeys).forEach((key) => selector(key, undefined));
const matched = patterns.some((p) => (0, helpers_1.matchPattern)(p, value, selector));
return { matched, selections };
},
getSelectionKeys: () => (0, helpers_1.flatMap)(patterns, helpers_1.getSelectionKeys),
matcherType: 'or',
}),
};
}
exports.union = union;

@@ -141,9 +145,11 @@ /**

*/
const not = (pattern) => ({
[symbols.matcher]: () => ({
match: (value) => ({ matched: !(0, helpers_1.matchPattern)(pattern, value, () => { }) }),
getSelectionKeys: () => [],
matcherType: 'not',
}),
});
function not(pattern) {
return {
[symbols.matcher]: () => ({
match: (value) => ({ matched: !(0, helpers_1.matchPattern)(pattern, value, () => { }) }),
getSelectionKeys: () => [],
matcherType: 'not',
}),
};
}
exports.not = not;

@@ -159,7 +165,9 @@ /**

*/
const when = (predicate) => ({
[symbols.matcher]: () => ({
match: (value) => ({ matched: predicate(value) }),
}),
});
function when(predicate) {
return {
[symbols.matcher]: () => ({
match: (value) => ({ matched: predicate(value) }),
}),
};
}
exports.when = when;

@@ -227,3 +235,3 @@ function select(...args) {

*/
exports.__ = (0, exports.when)(isUnknown);
exports.__ = when(isUnknown);
/**

@@ -236,3 +244,3 @@ * ### String wildcard

*/
exports.string = (0, exports.when)(isString);
exports.string = when(isString);
/**

@@ -245,3 +253,3 @@ * ### Number wildcard

*/
exports.number = (0, exports.when)(isNumber);
exports.number = when(isNumber);
/**

@@ -253,3 +261,3 @@ * ### Boolean wildcard

*/
exports.boolean = (0, exports.when)(isBoolean);
exports.boolean = when(isBoolean);
/**

@@ -261,3 +269,3 @@ * ### BigInt wildcard

*/
exports.bigint = (0, exports.when)(isBigInt);
exports.bigint = when(isBigInt);
/**

@@ -269,3 +277,3 @@ * ### Symbol wildcard

*/
exports.symbol = (0, exports.when)(isSymbol);
exports.symbol = when(isSymbol);
/**

@@ -277,3 +285,3 @@ * ### Nullish wildcard

*/
exports.nullish = (0, exports.when)(isNullish);
exports.nullish = when(isNullish);
/**

@@ -285,3 +293,25 @@ * ### instanceOf

*/
const instanceOf = (classConstructor) => (0, exports.when)(isInstanceOf(classConstructor));
function instanceOf(classConstructor) {
return when(isInstanceOf(classConstructor));
}
exports.instanceOf = instanceOf;
/**
* ### typed
* `P.typed<SomeType>()` is a way to set the input type this
* pattern should match.
*
* It returns all utility functions to create patterns,
* Like `array`, `union`, `intersection`, etc.
*/
function typed() {
return {
array: array,
optional: optional,
intersection: intersection,
union: union,
not: not,
select: select,
when: when,
};
}
exports.typed = typed;
import type * as symbols from '../internals/symbols';
import type { Cast, Equal, IsAny, UnionToIntersection } from './helpers';
import type { Cast, Equal, IsAny, TupleKeys, UnionToTuple } from './helpers';
import type { Matchable, Pattern } from './Pattern';

@@ -20,19 +20,12 @@ declare type SelectionsRecord = Record<string, [unknown, unknown[]]>;

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 '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[]>> : matcherType extends 'select' ? sel extends Some<infer k> ? {
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 : sel extends Some<infer k> ? {
} | 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 (infer pp)[] ? i extends readonly (infer ii)[] ? [i, p] extends [
readonly [infer i1, infer i2, infer i3, infer i4, infer i5],
readonly [infer p1, infer p2, infer p3, infer p4, infer p5]
] ? FindSelectionUnion<i1, p1, [...path, 1]> | FindSelectionUnion<i2, p2, [...path, 2]> | FindSelectionUnion<i3, p3, [...path, 3]> | FindSelectionUnion<i4, p4, [...path, 4]> | FindSelectionUnion<i5, p5, [...path, 5]> : [i, p] extends [
readonly [infer i1, infer i2, infer i3, infer i4],
readonly [infer p1, infer p2, infer p3, infer p4]
] ? FindSelectionUnion<i1, p1, [...path, 1]> | FindSelectionUnion<i2, p2, [...path, 2]> | FindSelectionUnion<i3, p3, [...path, 3]> | FindSelectionUnion<i4, p4, [...path, 4]> : [i, p] extends [
readonly [infer i1, infer i2, infer i3],
readonly [infer p1, infer p2, infer p3]
] ? FindSelectionUnion<i1, p1, [...path, 1]> | FindSelectionUnion<i2, p2, [...path, 2]> | FindSelectionUnion<i3, p3, [...path, 3]> : [i, p] extends [
readonly [infer i1, infer i2],
readonly [infer p1, infer p2]
] ? FindSelectionUnion<i1, p1, [...path, 1]> | FindSelectionUnion<i2, p2, [...path, 2]> : FindSelectionUnion<ii, pp, [...path, 1]> : never : p extends object ? i extends object ? {
} : never : p extends readonly [any, ...any[]] ? i extends readonly [any, ...any[]] ? {
[k in TupleKeys & keyof i & keyof p]: FindSelectionUnion<i[k], p[k], [
...path,
k
]>;
}[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 ? {
[k in keyof p]: k extends keyof i ? FindSelectionUnion<i[k], p[k], [...path, k]> : never;

@@ -46,5 +39,3 @@ }[keyof p] : never : never;

} & a;
export declare type SelectionToArgs<selections extends SelectionsRecord, i> = [
keyof selections
] extends [never] ? i : symbols.anonymousSelectKey extends keyof selections ? [
export declare type SelectionToArgs<selections extends SelectionsRecord> = symbols.anonymousSelectKey extends keyof selections ? [
selections[symbols.anonymousSelectKey][1]

@@ -54,4 +45,12 @@ ] extends [never] ? SeveralAnonymousSelectError : keyof selections extends symbols.anonymousSelectKey ? selections[symbols.anonymousSelectKey][0] : MixedNamedAndAnonymousSelectError : {

};
export declare type Selections<i, p> = Cast<UnionToIntersection<{} | FindSelectionUnion<i, p>>, SelectionsRecord>;
export declare type FindSelected<i, p> = Equal<p, Pattern<i>> extends true ? i : SelectionToArgs<Selections<i, p>, i>;
declare type ConcatSelections<a extends SelectionsRecord, b extends SelectionsRecord> = {
[k in keyof a & keyof b]: [a[k][0] | b[k][0], a[k][1] & b[k][1]];
} & {
[k in Exclude<keyof a, keyof b>]: a[k];
} & {
[k in Exclude<keyof b, keyof a>]: b[k];
};
declare type ReduceToRecord<selections extends any[], output extends SelectionsRecord = {}> = selections extends [infer sel, ...infer rest] ? ReduceToRecord<rest, ConcatSelections<Cast<sel, SelectionsRecord>, output>> : output;
export declare type Selections<i, p> = FindSelectionUnion<i, p> extends infer u ? [u] extends [never] ? i : SelectionToArgs<ReduceToRecord<UnionToTuple<u>>> : i;
export declare type FindSelected<i, p> = Equal<p, Pattern<i>> extends true ? i : Selections<i, p>;
export {};

@@ -41,2 +41,15 @@ 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;
}>;
} : never;
export declare type HasObjects<a> = true extends (a extends object ? IsPlainObject<a> : false) ? true : false;
export declare type FilterObjects<a> = a extends object ? IsPlainObject<a> extends true ? a : never : never;
export declare type ExcludeObjects<a> = a extends object ? IsPlainObject<a> extends true ? never : a : a;
export declare type PartitionObjects<a> = {
objects: FilterObjects<a>;
others: ExcludeObjects<a>;
};
export declare type All<xs> = xs extends readonly [infer head, ...infer tail] ? boolean extends head ? false : head extends true ? All<tail> : false : true;

@@ -47,2 +60,3 @@ export declare type Or<a extends boolean, b extends boolean> = true extends a | b ? true : false;

export declare type Primitives = number | boolean | string | undefined | null | symbol | bigint;
export declare type TupleKeys = 0 | 1 | 2 | 3 | 4;
export declare type Union<a, b> = [b] extends [a] ? a : [a] extends [b] ? b : a | b;

@@ -49,0 +63,0 @@ /**

@@ -17,3 +17,3 @@ import { DeepExclude } from './DeepExclude';

*/
export declare type InvertPattern<p> = p extends Matchable<infer input, infer narrowed, infer matcherType, any> ? matcherType extends 'not' ? ToExclude<InvertPattern<narrowed>> : matcherType extends 'array' ? InvertPattern<narrowed>[] : matcherType extends 'optional' ? InvertPattern<narrowed> | undefined : matcherType extends 'select' ? InvertPattern<narrowed> : 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> ? 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] ? [
InvertPattern<p1>,

@@ -39,3 +39,3 @@ InvertPattern<p2>,

*/
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 'array' ? i extends readonly (infer ii)[] ? InvertPatternForExclude<narrowed, ii, empty>[] : empty : matcherType extends 'optional' ? InvertPatternForExclude<narrowed, i, empty> | undefined : matcherType extends 'select' ? InvertPatternForExclude<narrowed, i, empty> : 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<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 [
InvertPatternForExclude<p1, i1, empty>,

@@ -59,6 +59,6 @@ InvertPatternForExclude<p2, i2, empty>,

] : empty : p extends readonly [infer p1] ? i extends readonly [infer i1] ? readonly [InvertPatternForExclude<p1, i1, empty>] : empty : p extends readonly [] ? [] : InvertPatternForExclude<pp, ii, empty>[] : empty : p extends Map<infer pk, infer pv> ? i extends Map<any, infer iv> ? Map<pk, InvertPatternForExclude<pv, iv, empty>> : empty : p extends Set<infer pv> ? i extends Set<infer iv> ? Set<InvertPatternForExclude<pv, iv, empty>> : empty : IsPlainObject<p> extends true ? i extends object ? [keyof p & keyof i] extends [never] ? empty : Compute<{
[k in Exclude<keyof p, OptionalKeys<p>>]: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
readonly [k in Exclude<keyof p, OptionalKeys<p>>]: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
} & {
[k in OptionalKeys<p>]?: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
readonly [k in OptionalKeys<p>]?: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
}> : empty : empty;
export {};
import type * as symbols from '../internals/symbols';
import { Primitives, Compute, Cast, IsPlainObject, IsUnion, ValueOf } from './helpers';
import { Primitives, Cast, IsUnion, HasObjects, PartitionObjects, PartitionKeys } from './helpers';
import { None, Some, SelectionType } from './FindSelected';

@@ -28,18 +28,5 @@ export declare type MatcherType = 'not' | 'optional' | 'or' | 'and' | 'array' | 'select' | 'default';

}
export declare type UnknownPattern = readonly [Pattern<unknown>, ...Pattern<unknown>[]] | {
readonly [k: string]: Pattern<unknown>;
} | Set<Pattern<unknown>> | Map<unknown, Pattern<unknown>> | Primitives | AnyMatchable;
declare type Keys<a> = keyof Compute<a> extends infer shared ? {
shared: shared;
others: ValueOf<{
[k in keyof a]: k extends shared ? never : k;
}>;
} : never;
declare type HasObjects<a> = true extends (a extends object ? IsPlainObject<a> : false) ? true : false;
declare type FilterObjects<a> = a extends object ? IsPlainObject<a> extends true ? a : never : never;
declare type ExcludeObjects<a> = a extends object ? IsPlainObject<a> extends true ? never : a : a;
declare type PartitionObjects<a> = {
objects: FilterObjects<a>;
others: ExcludeObjects<a>;
};
export declare type UnknownPattern = readonly [UnknownPattern, ...UnknownPattern[]] | {
readonly [k: string]: UnknownPattern;
} | Set<UnknownPattern> | Map<unknown, UnknownPattern> | Primitives | AnyMatchable;
declare type StructuralPattern<a> = [

@@ -51,10 +38,8 @@ IsUnion<a>,

others: infer othersValues;
} ? (Keys<objects> extends {
} ? (PartitionKeys<objects> extends {
shared: infer sharedKeys;
others: infer otherKeys;
} ? Compute<{
readonly [k in sharedKeys & keyof objects]?: Pattern<objects[k]>;
} & {
readonly [k in Cast<otherKeys, string>]?: objects extends any ? k extends keyof objects ? Pattern<objects[k]> : never : never;
}> : 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 [
} ? {
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;
} : 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 [
Pattern<a1>,

@@ -61,0 +46,0 @@ Pattern<a2>,

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

@@ -17,4 +17,6 @@ "main": "lib/index.js",

"test": "jest",
"clear-test": "jest --clearCache",
"perf": "tsc --project tests/tsconfig.json --noEmit --extendedDiagnostics",
"fmt": "prettier ./src/** ./tests/** -w"
"fmt": "prettier ./src/** ./tests/** -w",
"check": "tsc --strict --noEmit --extendedDiagnostics"
},

@@ -21,0 +23,0 @@ "files": [

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