UNPKG

variant

Version:

Variant types (a.k.a. Discriminated Unions) in TypeScript

37 lines 2.11 kB
/** * What should a progression do? * * - Be able to create a variant * - maybe this isn't necessary. * - do a .compare(a: T, b: T) and get back a number; */ import { Remote } from './remote'; import { Func } from './util'; import { KeysOf, Property, VariantCreator, VariantModule, VariantModuleFromList } from './variant'; export declare enum CompareResult { Lesser = -1, Equal = 0, Greater = 1 } declare type ValidTypeCreatorInput<K extends string = 'type', T extends string = string> = T | VariantCreator<T, Func, K>; declare type ValidTypeInput<K extends string = 'type', T extends string = string> = ValidTypeCreatorInput<K, T> | Property<K, T>; declare type ExtractType<T, K extends string = 'type'> = T extends ValidTypeInput<K> ? T extends string ? T : T extends VariantCreator<infer TType, Func, K> ? TType : T extends Property<K, infer TType> ? TType : never : never; declare type ExtractKeys<T extends readonly ValidTypeInput<K>[], K extends string = 'type'> = T extends readonly (infer R)[] ? ExtractType<R, K> : never; /** * @alpha * */ export interface Sequence<T extends VariantModule<K>, L extends ValidTypeInput<K, KeysOf<T, K>>[], K extends string = 'type', RT extends Pick<T, ExtractKeys<L, K>> = Pick<T, ExtractKeys<L, K>>> extends Remote<RT, K> { compare: (a: ValidTypeInput<K, KeysOf<RT, K>>, b: ValidTypeInput<K, KeysOf<RT, K>>) => CompareResult; /** * Get the index of some type in the sequence. */ index: (a: ValidTypeInput<K, KeysOf<RT, K>>) => number; get: (index: number) => T[keyof T]; readonly length: number; readonly types: KeysOf<T, K>[]; } export declare function sequence<L extends ValidTypeCreatorInput<'type', T>[], T extends string>(list: L): Sequence<VariantModuleFromList<L[number]>, ValidTypeInput<'type', KeysOf<VariantModuleFromList<L[number]>>>[]>; export declare function sequence<T extends VariantModule<K>, L extends ValidTypeInput<K, KeysOf<T, K>>[], K extends string = 'type'>(vmod: T, order: L): Sequence<T, L, K>; export {}; //# sourceMappingURL=sequence.d.ts.map