add generics for spected

This commit is contained in:
Benjamin Makus
2019-01-17 17:02:51 +01:00
parent b1790233a4
commit a916ab6997
+16 -8
View File
@@ -4,20 +4,28 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
declare function spected<ROOTINPUT, SPEC = Spec<ROOTINPUT>>(spec: SPEC, input: ROOTINPUT): Result<ROOTINPUT, SPEC>;
declare function spected<ROOTINPUT, SPEC = SpecObject<ROOTINPUT, ROOTINPUT>>(spec: SPEC, input: ROOTINPUT): Result<ROOTINPUT, SPEC>;
export type Predicate<INPUT, ROOTINPUT> = (value: INPUT, inputs: ROOTINPUT) => boolean;
type Predicate<INPUT, ROOTINPUT> = (value: INPUT, inputs: ROOTINPUT) => boolean;
export type ErrorMsg<INPUT> =
type ErrorMsg<INPUT> =
| (string | number | boolean | symbol | null | undefined | object)
| ((value: INPUT, field: string) => any);
export type Spec<INPUT, ROOTINPUT = any> = Partial<{[key in keyof INPUT]: SpecValue<INPUT[key], ROOTINPUT>}>;
type SpecArrayElement<INPUT, ROOTINPUT> = [Predicate<INPUT, ROOTINPUT>, ErrorMsg<INPUT>];
export type SpecValue<INPUT, ROOTINPUT = any> =
| ReadonlyArray<[Predicate<INPUT, ROOTINPUT>, ErrorMsg<INPUT>]>
| ((value: INPUT) => any)
| Spec<INPUT, ROOTINPUT>;
export type SpecArray<INPUT, ROOTINPUT> = ReadonlyArray<SpecArrayElement<INPUT, ROOTINPUT>>;
export type SpecFunction<INPUT, ROOTINPUT> = INPUT extends ReadonlyArray<infer U>
? (value: INPUT) => ReadonlyArray<SpecArray<U, ROOTINPUT>>
: (value: INPUT) => SpecObject<INPUT, ROOTINPUT>;
export type SpecObject<INPUT, ROOTINPUT> = Partial<{[key in keyof INPUT]: SpecValue<INPUT[key], ROOTINPUT>}>;
export type SpecValue<INPUT, ROOTINPUT> =
| SpecArray<INPUT, ROOTINPUT>
| SpecFunction<INPUT, ROOTINPUT>
| SpecObject<INPUT, ROOTINPUT>;
export type Result<INPUT, SPEC> = {[key in keyof INPUT]: true | any[] | Result<INPUT[key], any>};