From a916ab6997d73622bdda0de9e02f0747cd16f6d7 Mon Sep 17 00:00:00 2001 From: Benjamin Makus Date: Thu, 17 Jan 2019 17:02:51 +0100 Subject: [PATCH] add generics for spected --- types/spected/index.d.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/types/spected/index.d.ts b/types/spected/index.d.ts index 99b987a0c3..7757a05aed 100644 --- a/types/spected/index.d.ts +++ b/types/spected/index.d.ts @@ -4,20 +4,28 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 -declare function spected>(spec: SPEC, input: ROOTINPUT): Result; +declare function spected>(spec: SPEC, input: ROOTINPUT): Result; -export type Predicate = (value: INPUT, inputs: ROOTINPUT) => boolean; +type Predicate = (value: INPUT, inputs: ROOTINPUT) => boolean; -export type ErrorMsg = +type ErrorMsg = | (string | number | boolean | symbol | null | undefined | object) | ((value: INPUT, field: string) => any); -export type Spec = Partial<{[key in keyof INPUT]: SpecValue}>; +type SpecArrayElement = [Predicate, ErrorMsg]; -export type SpecValue = - | ReadonlyArray<[Predicate, ErrorMsg]> - | ((value: INPUT) => any) - | Spec; +export type SpecArray = ReadonlyArray>; + +export type SpecFunction = INPUT extends ReadonlyArray + ? (value: INPUT) => ReadonlyArray> + : (value: INPUT) => SpecObject; + +export type SpecObject = Partial<{[key in keyof INPUT]: SpecValue}>; + +export type SpecValue = + | SpecArray + | SpecFunction + | SpecObject; export type Result = {[key in keyof INPUT]: true | any[] | Result};