optix
Version:
Optix is a data manipulation library that provides optics-like capabilites with a simpler syntax. It features robust Typescript support with minimal type annotations and is smaller and faster than true optics libraries.
16 lines (15 loc) • 1.26 kB
TypeScript
import { BuildTypeFromPath, BuildTypeFromPathOptional, GetTypeAtPath, MergeRightWthDepth, PathItem } from "./common";
declare type IfElse<B extends boolean, If, Else> = B extends true ? If : Else;
declare type OptionalKeys<T> = {
[K in keyof T]-?: Record<string, unknown> extends Pick<T, K> ? K : never;
}[keyof T];
declare type IsOptional<T, K extends string> = string extends keyof T ? true : [OptionalKeys<T>] extends [never] ? false : K extends OptionalKeys<T> ? true : false;
declare type IsRemovable<T, K extends PathItem> = Exclude<T, undefined> extends any[] ? true : string extends keyof T ? true : K extends string ? IsOptional<T, K> : false;
export interface Remove {
<Keys extends readonly [PathItem, ...PathItem[]]>(...keys: Keys): <T extends BuildTypeFromPathOptional<unknown, Keys>>(obj: T) => Keys extends [...infer Sliced, infer Last] ? IfElse<IsRemovable<GetTypeAtPath<T, Sliced>, Last>, T, unknown> : T;
}
export interface PolyRemove {
<Keys extends readonly [PathItem, ...PathItem[]]>(...keys: Keys): <T>(obj: T) => Keys extends [infer _, ...infer Rest] ? MergeRightWthDepth<T, BuildTypeFromPath<never, Keys>, Rest> : T;
}
export declare type _Remove = <T>(obj: T, path: readonly PathItem[]) => T;
export {};