UNPKG

1.58 kBTypeScriptView Raw
1export declare namespace OptionParser {
2 type MismatchCallback = (type: string) => void;
3 type ParseFunction<T> = (value: any, report: MismatchCallback) => T;
4 type ParsedOptions<T extends Record<string, ParseFunction<any>>> = {
5 [K in keyof T]: ReturnType<T[K]>;
6 };
7 interface ParseConfig {
8 validate?: boolean;
9 context: string;
10 exhaustive?: boolean;
11 }
12 function parse<T extends Record<string, ParseFunction<any>>>(options: Record<string, any> | undefined, specs: T, config: ParseConfig): ParsedOptions<T>;
13 namespace Transform {
14 function withDefault<T>(parseFn: ParseFunction<T | undefined>, defaultValue: T): ParseFunction<T>;
15 function noDefault<T>(parseFn: ParseFunction<T>): ParseFunction<T | undefined>;
16 function map<T extends ReadonlyArray<U> | undefined, U, V>(parseFn: ParseFunction<T>, cb: (item: U) => V): ParseFunction<{
17 -readonly [K in keyof T]: V;
18 }>;
19 function transform<T, U>(parseFn: ParseFunction<T>, cb: (value: T) => U): ParseFunction<U>;
20 }
21 namespace Factory {
22 type PrimitiveName = 'string' | 'number' | 'boolean';
23 type PrimitiveMap<T extends PrimitiveName> = T extends 'string' ? string : T extends 'number' ? number : boolean;
24 export function parsePrimitive<T extends PrimitiveName[]>(...types: T): ParseFunction<PrimitiveMap<T[number]> | undefined>;
25 export function parsePrimitiveOrArray<T extends PrimitiveName>(type: T): ParseFunction<ReadonlyArray<PrimitiveMap<T>> | undefined>;
26 export {};
27 }
28}