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.6 to 4.1.0

dist/index.modern.mjs

2

dist/types/helpers.d.ts

@@ -55,1 +55,3 @@ export declare type ValueOf<a> = a extends any[] ? a[number] : a[keyof a];

export declare type GuardFunction<input, narrowed> = ((value: input) => value is Cast<narrowed, input>) | ((value: input) => boolean);
export declare type Some<bools extends boolean[]> = true extends bools[number] ? true : false;
export declare type All<bools extends boolean[]> = bools[number] extends true ? true : false;

57

dist/types/InvertPattern.d.ts
import { DeepExclude } from './DeepExclude';
import { IsPlainObject, Primitives, IsLiteral, ValueOf, Compute, Cast, Equal } from './helpers';
import type { Matcher, ToExclude } from './Pattern';
import type { Matcher, Pattern, ToExclude } from './Pattern';
declare type OptionalKeys<p> = ValueOf<{

@@ -43,41 +43,44 @@ [k in keyof p]: p[k] extends Matcher<any, any, infer matcherType> ? matcherType extends 'optional' ? k : never : never;

}> : never : p;
export declare type ReduceIntersectionForExclude<tuple extends any[], i, output = unknown> = tuple extends readonly [infer p, ...infer tail] ? ReduceIntersectionForExclude<tail, i, output & InvertPatternForExclude<p, i, unknown>> : output;
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;
export declare type ReduceIntersectionForExclude<tuple extends any[], i, output = unknown> = tuple extends readonly [infer p, ...infer tail] ? ReduceIntersectionForExclude<tail, i, output & InvertPatternForExcludeInternal<p, i, unknown>> : output;
export declare type ReduceUnionForExclude<tuple extends any[], i, output = never> = tuple extends readonly [infer p, ...infer tail] ? ReduceUnionForExclude<tail, i, output | InvertPatternForExcludeInternal<p, i, never>> : output;
declare type ExcludeIfExists<a, b> = [
b
] extends [never] ? never : unknown extends a ? never : DeepExclude<a, b>;
/**
* ### InvertPatternForExclude
*/
export declare type InvertPatternForExclude<p, i, empty = never> = p extends Matcher<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;
export declare type InvertPatternForExclude<p, i> = Equal<p, Pattern<i>> extends true ? never : InvertPatternForExcludeInternal<p, i>;
declare type InvertPatternForExcludeInternal<p, i, empty = never> = p extends Matcher<infer matchableInput, infer subpattern, infer matcherType, any, infer excluded> ? {
select: InvertPatternForExcludeInternal<subpattern, i, empty>;
array: i extends readonly (infer ii)[] ? InvertPatternForExcludeInternal<subpattern, ii, empty>[] : empty;
optional: InvertPatternForExcludeInternal<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>>;
not: ExcludeIfExists<unknown extends matchableInput ? i : matchableInput, InvertPatternForExcludeInternal<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>,
InvertPatternForExclude<p2, i2, empty>,
InvertPatternForExclude<p3, i3, empty>,
InvertPatternForExclude<p4, i4, empty>,
InvertPatternForExclude<p5, i5, empty>
InvertPatternForExcludeInternal<p1, i1, empty>,
InvertPatternForExcludeInternal<p2, i2, empty>,
InvertPatternForExcludeInternal<p3, i3, empty>,
InvertPatternForExcludeInternal<p4, i4, empty>,
InvertPatternForExcludeInternal<p5, i5, empty>
] : empty : p extends readonly [infer p1, infer p2, infer p3, infer p4] ? i extends readonly [infer i1, infer i2, infer i3, infer i4] ? readonly [
InvertPatternForExclude<p1, i1, empty>,
InvertPatternForExclude<p2, i2, empty>,
InvertPatternForExclude<p3, i3, empty>,
InvertPatternForExclude<p4, i4, empty>
InvertPatternForExcludeInternal<p1, i1, empty>,
InvertPatternForExcludeInternal<p2, i2, empty>,
InvertPatternForExcludeInternal<p3, i3, empty>,
InvertPatternForExcludeInternal<p4, i4, empty>
] : empty : p extends readonly [infer p1, infer p2, infer p3] ? i extends readonly [infer i1, infer i2, infer i3] ? readonly [
InvertPatternForExclude<p1, i1, empty>,
InvertPatternForExclude<p2, i2, empty>,
InvertPatternForExclude<p3, i3, empty>
InvertPatternForExcludeInternal<p1, i1, empty>,
InvertPatternForExcludeInternal<p2, i2, empty>,
InvertPatternForExcludeInternal<p3, i3, empty>
] : empty : p extends readonly [infer p1, infer p2] ? i extends readonly [infer i1, infer i2] ? readonly [
InvertPatternForExclude<p1, i1, empty>,
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 : OptionalKeys<p> extends infer optKeys ? [optKeys] extends [never] ? {
readonly [k in keyof p]: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
InvertPatternForExcludeInternal<p1, i1, empty>,
InvertPatternForExcludeInternal<p2, i2, empty>
] : empty : p extends readonly [infer p1] ? i extends readonly [infer i1] ? readonly [InvertPatternForExcludeInternal<p1, i1, empty>] : empty : p extends readonly [] ? [] : InvertPatternForExcludeInternal<pp, ii, empty>[] : empty : p extends Map<infer pk, infer pv> ? i extends Map<any, infer iv> ? Map<pk, InvertPatternForExcludeInternal<pv, iv, empty>> : empty : p extends Set<infer pv> ? i extends Set<infer iv> ? Set<InvertPatternForExcludeInternal<pv, iv, empty>> : empty : IsPlainObject<p> extends true ? i extends object ? [keyof p & keyof i] extends [never] ? empty : OptionalKeys<p> extends infer optKeys ? [optKeys] extends [never] ? {
readonly [k in keyof p]: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPattern<p[k]>;
} : Compute<{
readonly [k in Exclude<keyof p, optKeys>]: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
readonly [k in Exclude<keyof p, optKeys>]: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPattern<p[k]>;
} & {
readonly [k in Cast<optKeys, keyof p>]?: k extends keyof i ? InvertPatternForExclude<p[k], i[k], empty> : InvertPattern<p[k]>;
readonly [k in Cast<optKeys, keyof p>]?: k extends keyof i ? InvertPatternForExcludeInternal<p[k], i[k], empty> : InvertPattern<p[k]>;
}> : empty : empty : empty;
export {};
import type * as symbols from '../internals/symbols';
import type { Pattern, Matcher } from './Pattern';
import type { Pattern } from './Pattern';
import type { ExtractPreciseValue } from './ExtractPreciseValue';

@@ -17,3 +17,3 @@ import type { InvertPatternForExclude, InvertPattern } from './InvertPattern';

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

@@ -25,8 +25,14 @@ * `.with(pattern, handler)` Registers a pattern and an handler function which

**/
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<p1 extends Pattern<i>, p2 extends Pattern<i>, 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, [
with<p extends Pattern<i>, c, value extends MatchedValue<i, InvertPattern<p>>, x = InvertPatternForExclude<p, value>>(pattern: p, handler: (selections: FindSelected<value, p>, value: value) => PickReturnValue<o, c>): [InvertPatternForExclude<p, value>] extends [infer excluded] ? Match<Exclude<i, excluded>, o, [
...patternValueTuples,
[p1, value],
[p2, value]
], Union<inferredOutput, c>>;
excluded
], Union<inferredOutput, c>> : never;
with<p1 extends Pattern<i>, p2 extends Pattern<i>, 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>): [
InvertPatternForExclude<p1, value>,
InvertPatternForExclude<p2, value>
] extends [infer excluded1, infer excluded2] ? Match<Exclude<i, excluded1 | excluded2>, o, [
...patternValueTuples,
excluded1,
excluded2
], Union<inferredOutput, c>> : never;
with<p1 extends Pattern<i>, p2 extends Pattern<i>, p3 extends Pattern<i>, ps extends Pattern<i>[], c, p extends p1 | p2 | p3 | ps[number], value extends p extends any ? MatchedValue<i, InvertPattern<p>> : never>(...args: [

@@ -38,19 +44,23 @@ p1: p1,

handler: (value: value) => PickReturnValue<o, c>
]): Match<i, o, [
]): [
InvertPatternForExclude<p1, value>,
InvertPatternForExclude<p2, value>,
InvertPatternForExclude<p3, value>,
MakeTuples<ps, value>
] extends [
infer excluded1,
infer excluded2,
infer excluded3,
infer excludedRest extends any[]
] ? Match<Exclude<i, excluded1 | excluded2 | excluded3 | excludedRest[number]>, o, [
...patternValueTuples,
[
p1,
value
],
[
p2,
value
],
[
p3,
value
],
...MakeTuples<ps, 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, [Matcher<unknown, narrowed>, value]] : patternValueTuples, Union<inferredOutput, c>>;
excluded1,
excluded2,
excluded3,
...excludedRest
], Union<inferredOutput, c>> : never;
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>): pred extends (value: any) => value is infer narrowed ? Match<Exclude<i, narrowed>, o, [
...patternValueTuples,
narrowed
], Union<inferredOutput, c>> : Match<i, o, patternValueTuples, Union<inferredOutput, c>>;
/**

@@ -62,3 +72,6 @@ * `.when(predicate, handler)` Registers a predicate function and an handler function.

**/
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, [Matcher<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>): pred extends (value: any) => value is infer narrowed ? Match<Exclude<i, narrowed>, o, [
...patternValueTuples,
narrowed
], Union<inferredOutput, c>> : Match<i, o, patternValueTuples, Union<inferredOutput, c>>;
/**

@@ -107,12 +120,6 @@ * `.otherwise()` takes a function returning the **default value**, and

*/
declare type DeepExcludeAll<a, tupleList extends any[]> = tupleList extends [
[
infer p,
infer v
],
...infer tail
] ? DeepExcludeAll<DeepExclude<a, InvertPatternForExclude<p, v>>, tail> : a;
declare type DeepExcludeAll<a, tupleList extends any[]> = [a] extends [never] ? never : tupleList extends [infer matched, ...infer tail] ? DeepExcludeAll<DeepExclude<a, matched>, tail> : a;
declare type MakeTuples<ps extends any[], value> = {
-readonly [index in keyof ps]: [ps[index], value];
-readonly [index in keyof ps]: InvertPatternForExclude<ps[index], value>;
};
export {};
{
"name": "ts-pattern",
"version": "4.0.6",
"version": "4.1.0",
"description": " The exhaustive Pattern Matching library for TypeScript.",

@@ -27,2 +27,3 @@ "type": "module",

"dev": "microbundle watch",
"prepublishOnly": "npm run test && npm run build",
"test": "jest",

@@ -29,0 +30,0 @@ "clear-test": "jest --clearCache",

@@ -93,4 +93,2 @@ <h1 align="center">TS-Pattern</h1>

#### ⚠️ This is the docs for **TS-Pattern v4**. Find the docs for [**TS-Pattern v3 here**](https://github.com/gvergnaud/ts-pattern/tree/v3).
- [Code Sandbox Examples](#code-sandbox-examples)

@@ -97,0 +95,0 @@ - [Getting Started](#getting-started)

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

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