UNPKG

@cuppachino/ts-pattern

Version:

The exhaustive Pattern Matching library for TypeScript.

18 lines (17 loc) 1.18 kB
import { Cast, Compute, Iterator, UpdateAt } from './helpers.js'; export declare type BuildMany<data, xs extends any[]> = xs extends any ? BuildOne<data, xs> : never; declare type BuildOne<data, xs extends any[]> = xs extends [ [ infer value, infer path ], ...infer tail ] ? BuildOne<Update<data, value, Cast<path, PropertyKey[]>>, tail> : data; declare type SafeGet<data, k extends PropertyKey, def> = k extends keyof data ? data[k] : def; declare type Update<data, value, path extends PropertyKey[]> = path extends [ infer head, ...infer tail ] ? data extends readonly [any, ...any] ? head extends number ? UpdateAt<data, Iterator<head>, Update<data[head], value, Cast<tail, PropertyKey[]>>> : never : data extends readonly (infer a)[] ? Update<a, value, Cast<tail, PropertyKey[]>>[] : data extends Set<infer a> ? Set<Update<a, value, Cast<tail, PropertyKey[]>>> : data extends Map<infer k, infer v> ? Map<k, Update<v, value, Cast<tail, PropertyKey[]>>> : Compute<Omit<data, Cast<head, PropertyKey>> & { [k in Cast<head, PropertyKey>]: Update<SafeGet<data, k, {}>, value, Cast<tail, PropertyKey[]>>; }> : value; export {};