variant
Version:
Variant types (a.k.a. Discriminated Unions) in TypeScript
24 lines • 1.36 kB
TypeScript
import { Property, VariantsOfUnion } from './variant';
/**
* An object that has the same keys as a variant but has arbitrary values for the data.
* a.k.a. a lookup table.
*/
export declare type Lookup<T, U = any> = {
[P in keyof T]: U;
};
/**
* Process an unknown variant by comparing it to a provided lookup table and returning the proper result.
* @param obj some object that extends `{[K]: string}`.
* @param handler a lookup table going from the various keys of `obj`'s type to any.
* @param typeKey the key used as the discriminant.
*/
export declare function lookup<T extends Property<K, string>, L extends Lookup<VariantsOfUnion<T, K>>, K extends string = 'type'>(obj: T, handler: L, typeKey?: K): L[keyof L];
/**
* Process an unknown variant by comparing it to a provided lookup table and returning the proper result.
* If the handler does not account for the case, returns undefined
* @param obj some object that extends `{[K]: string}`.
* @param handler a partial lookup table going from some keys of `obj`'s type to any.
* @param typeKey the key used as the discriminant.
*/
export declare function partialLookup<T extends Property<K, string>, L extends Lookup<VariantsOfUnion<T, K>>, K extends string = 'type'>(obj: T, handler: Partial<L>, typeKey?: K): L | undefined;
//# sourceMappingURL=lookup.d.ts.map