From 3f0d47355b91597eceab01da9da9abcd69ed9a46 Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Mon, 30 Nov 2020 20:44:44 -0600 Subject: [PATCH] feat: add flow defs [WIP] --- .flowconfig | 11 + package.json | 1 + src/Validation.js.flow | 29 + src/errorReporting/RuntimeTypeError.js.flow | 10 + src/index.js.flow | 209 +++ src/oneOf.js.flow | 1447 +++++++++++++++++++ src/typeConstraints.js.flow | 10 + src/types/AnyType.js.flow | 7 + src/types/ArrayType.js.flow | 11 + src/types/BooleanLiteralType.js.flow | 11 + src/types/BooleanType.js.flow | 7 + src/types/InstanceOfType.js.flow | 11 + src/types/IntersectionType.js.flow | 11 + src/types/NullLiteralType.js.flow | 7 + src/types/NumberType.js.flow | 7 + src/types/NumericLiteralType.js.flow | 10 + src/types/ObjectType.js.flow | 17 + src/types/ObjectTypeProperty.js.flow | 27 + src/types/ObjectTypeProperty.ts | 2 - src/types/RecordType.js.flow | 14 + src/types/StringLiteralType.js.flow | 11 + src/types/StringType.js.flow | 7 + src/types/SymbolLiteralType.js.flow | 11 + src/types/SymbolLiteralType.ts | 2 - src/types/SymbolType.js.flow | 7 + src/types/TupleType.js.flow | 11 + src/types/Type.js.flow | 29 + src/types/TypeAlias.js.flow | 18 + src/types/TypeReference.js.flow | 12 + src/types/UndefinedLiteralType.js.flow | 7 + src/types/UnionType.js.flow | 11 + tsconfig.json | 2 +- yarn.lock | 5 + 33 files changed, 1987 insertions(+), 5 deletions(-) create mode 100644 .flowconfig create mode 100644 src/Validation.js.flow create mode 100644 src/errorReporting/RuntimeTypeError.js.flow create mode 100644 src/index.js.flow create mode 100644 src/oneOf.js.flow create mode 100644 src/typeConstraints.js.flow create mode 100644 src/types/AnyType.js.flow create mode 100644 src/types/ArrayType.js.flow create mode 100644 src/types/BooleanLiteralType.js.flow create mode 100644 src/types/BooleanType.js.flow create mode 100644 src/types/InstanceOfType.js.flow create mode 100644 src/types/IntersectionType.js.flow create mode 100644 src/types/NullLiteralType.js.flow create mode 100644 src/types/NumberType.js.flow create mode 100644 src/types/NumericLiteralType.js.flow create mode 100644 src/types/ObjectType.js.flow create mode 100644 src/types/ObjectTypeProperty.js.flow create mode 100644 src/types/RecordType.js.flow create mode 100644 src/types/StringLiteralType.js.flow create mode 100644 src/types/StringType.js.flow create mode 100644 src/types/SymbolLiteralType.js.flow create mode 100644 src/types/SymbolType.js.flow create mode 100644 src/types/TupleType.js.flow create mode 100644 src/types/Type.js.flow create mode 100644 src/types/TypeAlias.js.flow create mode 100644 src/types/TypeReference.js.flow create mode 100644 src/types/UndefinedLiteralType.js.flow create mode 100644 src/types/UnionType.js.flow diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 0000000..1fed445 --- /dev/null +++ b/.flowconfig @@ -0,0 +1,11 @@ +[ignore] + +[include] + +[libs] + +[lints] + +[options] + +[strict] diff --git a/package.json b/package.json index 47c42d1..766cdc2 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,7 @@ "eslint": "^5.9.0", "eslint-config-prettier": "^3.3.0", "eslint-watch": "^4.0.2", + "flow-bin": "^0.138.0", "husky": "^1.1.4", "istanbul": "^0.4.5", "lint-staged": "^8.0.4", diff --git a/src/Validation.js.flow b/src/Validation.js.flow new file mode 100644 index 0000000..fc3d8ee --- /dev/null +++ b/src/Validation.js.flow @@ -0,0 +1,29 @@ +// @flow + +import Type from './types/Type' + +export type IdentifierPath = Array + +export type ErrorTuple = [IdentifierPath, string, Type] + +declare class Validation { + input: T; + + path: IdentifierPath; + + prefix: string; + + errors: ErrorTuple[]; + + constructor(input: T, prefix?: string, path?: IdentifierPath): void; + + inCycle(type: Type, input: any): boolean; + + startCycle(type: Type, input: any): void; + + endCycle(type: Type, input: any): void; + + hasErrors(): boolean; +} + +export default Validation diff --git a/src/errorReporting/RuntimeTypeError.js.flow b/src/errorReporting/RuntimeTypeError.js.flow new file mode 100644 index 0000000..59b9cf8 --- /dev/null +++ b/src/errorReporting/RuntimeTypeError.js.flow @@ -0,0 +1,10 @@ +// @flow + +import { type ErrorTuple } from '../Validation' + +declare class RuntimeTypeError extends TypeError { + errors: ?(ErrorTuple[]); + constructor(message: string, options?: { errors?: ?(ErrorTuple[]) }): void; +} + +export default RuntimeTypeError diff --git a/src/index.js.flow b/src/index.js.flow new file mode 100644 index 0000000..ae88050 --- /dev/null +++ b/src/index.js.flow @@ -0,0 +1,209 @@ +// @flow + +import Type from './types/Type' +import AnyType from './types/AnyType' +import ArrayType from './types/ArrayType' +import BooleanLiteralType from './types/BooleanLiteralType' +import BooleanType from './types/BooleanType' +import InstanceOfType from './types/InstanceOfType' +import IntersectionType from './types/IntersectionType' +import NullLiteralType from './types/NullLiteralType' +import UndefinedLiteralType from './types/UndefinedLiteralType' +import NumberType from './types/NumberType' +import NumericLiteralType from './types/NumericLiteralType' +import ObjectType from './types/ObjectType' +import ObjectTypeProperty from './types/ObjectTypeProperty' +import RecordType from './types/RecordType' +import StringLiteralType from './types/StringLiteralType' +import StringType from './types/StringType' +import SymbolLiteralType from './types/SymbolLiteralType' +import SymbolType from './types/SymbolType' +import TupleType from './types/TupleType' +import UnionType from './types/UnionType' +import TypeAlias from './types/TypeAlias' +import TypeReference from './types/TypeReference' +import Validation from './Validation' +import RuntimeTypeError from './errorReporting/RuntimeTypeError' +import oneOf from './oneOf' + +export { + Type, + AnyType, + ArrayType, + BooleanLiteralType, + BooleanType, + InstanceOfType, + IntersectionType, + NullLiteralType, + UndefinedLiteralType, + NumberType, + NumericLiteralType, + ObjectType, + ObjectTypeProperty, + RecordType, + StringLiteralType, + StringType, + SymbolLiteralType, + SymbolType, + TupleType, + UnionType, + TypeAlias, + TypeReference, + Validation, + RuntimeTypeError, + oneOf, +} + +declare export function any(): Type + +declare export function array(elementType: Type): Type + +declare export function nullLiteral(): Type +export { nullLiteral as null } +declare export function nullOr(type: Type): Type + +declare export function undefinedLiteral(): Type +export { undefinedLiteral as undefined } + +declare export function nullish(type: Type): Type +declare export function nullishOr(type: Type): Type + +declare export function boolean(): Type +declare export function boolean(literal: T): Type +declare export function boolean( + literal?: boolean +): Type | Type | Type + +declare export function number(): Type +declare export function number(literal: T): Type +declare export function number( + literal?: number +): Type | Type | Type + +declare export function string(): Type +declare export function string(literal: T): Type +declare export function string( + literal?: string +): Type | Type | Type + +declare export function symbol(): Type +declare export function symbol(literal: T): Type +declare export function symbol( + literal?: symbol +): Type | Type | Type + +type OptionalProperty = { __optional__: Type } + +declare export function optional(type: Type): OptionalProperty + +declare export function optionalNullOr( + type: Type +): OptionalProperty + +type OptionalKeys = { + [K in keyof T]: T extends Record ? never : K +} extends { + [_ in keyof T]: infer U +} + ? {} extends U + ? never + : U + : never + +export const object = >({ + exact, +}: { exact?: boolean } = {}) => < + P extends { + [K in keyof S]-?: K extends OptionalKeys + ? OptionalProperty + : Type + } +>( + properties: P +): ObjectType< + { + [K in keyof S]: P[K] extends OptionalProperty + ? T + : P[K] extends Type + ? T + : never + } +> => + new ObjectType( + [...Object.entries(properties)].map( + ([key, type]) => + new ObjectTypeProperty( + key, + getOptional(type) || (type as Type), + Boolean(getOptional(type)) + ) + ), + exact + ) as any + +type Properties = { [string | number | symbol]: Type } + +declare export function simpleObject( + required: Required, + options: ?{ exact?: boolean } +): ObjectType<$ObjMap(T) => $PropertyType>> + +declare export function record( + key: Type, + value: Type +): RecordType + +declare export function instanceOf(classType: { + new(...args: any[]): T, +}): Type + +declare export function tuple[]>( + ...types: T +): Type<$TupleMap(T) => $PropertyType>> + +declare export function allOf(...types: [Type]): Type +declare export function allOf( + ...types: [Type, Type] +): Type +declare export function allOf( + ...types: [Type, Type, Type] +): Type +declare export function allOf( + ...types: [Type, Type, Type, Type] +): Type +declare export function allOf( + ...types: [Type, Type, Type, Type, Type] +): Type +declare export function allOf( + ...types: [Type, Type, Type, Type, Type, Type] +): Type +declare export function allOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type +declare export function allOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type +declare export function allOf(...types: Type[]): Type + +declare export function alias(name: string, type: Type): TypeAlias + +declare export function ref(type: () => TypeAlias): Type + +export type ExtractType> = $PropertyType diff --git a/src/oneOf.js.flow b/src/oneOf.js.flow new file mode 100644 index 0000000..19495d1 --- /dev/null +++ b/src/oneOf.js.flow @@ -0,0 +1,1447 @@ +// @flow + +import Type from './types/Type' +import UnionType from './types/UnionType' + +declare function oneOf(...types: [Type]): Type + +declare function oneOf(...types: [Type, Type]): Type + +declare function oneOf( + ...types: [Type, Type, Type] +): Type + +declare function oneOf( + ...types: [Type, Type, Type, Type] +): Type + +declare function oneOf( + ...types: [Type, Type, Type, Type, Type] +): Type + +declare function oneOf( + ...types: [Type, Type, Type, Type, Type, Type] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 | T11 | T12 | T13 | T14 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 | T11 | T12 | T13 | T14 | T15 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26, + T27 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 + | T27 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26, + T27, + T28 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 + | T27 + | T28 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26, + T27, + T28, + T29 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 + | T27 + | T28 + | T29 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26, + T27, + T28, + T29, + T30 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 + | T27 + | T28 + | T29 + | T30 +> + +declare function oneOf< + T1, + T2, + T3, + T4, + T5, + T6, + T7, + T8, + T9, + T10, + T11, + T12, + T13, + T14, + T15, + T16, + T17, + T18, + T19, + T20, + T21, + T22, + T23, + T24, + T25, + T26, + T27, + T28, + T29, + T30, + T31 +>( + ...types: [ + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type, + Type + ] +): Type< + | T1 + | T2 + | T3 + | T4 + | T5 + | T6 + | T7 + | T8 + | T9 + | T10 + | T11 + | T12 + | T13 + | T14 + | T15 + | T16 + | T17 + | T18 + | T19 + | T20 + | T21 + | T22 + | T23 + | T24 + | T25 + | T26 + | T27 + | T28 + | T29 + | T30 + | T31 +> + +declare function oneOf(...types: Type[]): Type + +export default oneOf diff --git a/src/typeConstraints.js.flow b/src/typeConstraints.js.flow new file mode 100644 index 0000000..fda0283 --- /dev/null +++ b/src/typeConstraints.js.flow @@ -0,0 +1,10 @@ +// @flow + +import Type from './types/Type' +import Validation, { type ErrorTuple, type IdentifierPath } from './Validation' + +export type TypeConstraint = (input: T) => ?string + +export interface ConstrainableType extends Type { + constraints: TypeConstraint[]; +} diff --git a/src/types/AnyType.js.flow b/src/types/AnyType.js.flow new file mode 100644 index 0000000..d0d6e18 --- /dev/null +++ b/src/types/AnyType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class AnyType extends Type {} + +export default AnyType diff --git a/src/types/ArrayType.js.flow b/src/types/ArrayType.js.flow new file mode 100644 index 0000000..acfeabf --- /dev/null +++ b/src/types/ArrayType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class ArrayType extends Type> { + elementType: Type; + + constructor(elementType: Type): void; +} + +export default ArrayType diff --git a/src/types/BooleanLiteralType.js.flow b/src/types/BooleanLiteralType.js.flow new file mode 100644 index 0000000..18bd218 --- /dev/null +++ b/src/types/BooleanLiteralType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class BooleanLiteralType extends Type { + value: T; + + constructor(value: T): void; +} + +export default BooleanLiteralType diff --git a/src/types/BooleanType.js.flow b/src/types/BooleanType.js.flow new file mode 100644 index 0000000..990b845 --- /dev/null +++ b/src/types/BooleanType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class BooleanType extends Type {} + +export default BooleanType diff --git a/src/types/InstanceOfType.js.flow b/src/types/InstanceOfType.js.flow new file mode 100644 index 0000000..43e0a6a --- /dev/null +++ b/src/types/InstanceOfType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class InstanceOfType> extends Type { + classType: T; + + constructor(classType: T): void; +} + +export default InstanceOfType diff --git a/src/types/IntersectionType.js.flow b/src/types/IntersectionType.js.flow new file mode 100644 index 0000000..afcd22d --- /dev/null +++ b/src/types/IntersectionType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class IntersectionType extends Type { + types: Type[]; + + constructor(types: Type[]): void; +} + +export default IntersectionType diff --git a/src/types/NullLiteralType.js.flow b/src/types/NullLiteralType.js.flow new file mode 100644 index 0000000..210330f --- /dev/null +++ b/src/types/NullLiteralType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class NullLiteralType extends Type {} + +export default NullLiteralType diff --git a/src/types/NumberType.js.flow b/src/types/NumberType.js.flow new file mode 100644 index 0000000..0ee47a6 --- /dev/null +++ b/src/types/NumberType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class NumberType extends Type {} + +export default NumberType diff --git a/src/types/NumericLiteralType.js.flow b/src/types/NumericLiteralType.js.flow new file mode 100644 index 0000000..91fae58 --- /dev/null +++ b/src/types/NumericLiteralType.js.flow @@ -0,0 +1,10 @@ +// @flow + +import Type from './Type' + +declare class NumericLiteralType extends Type { + value: T; + + constructor(value: T): void; +} +export default NumericLiteralType diff --git a/src/types/ObjectType.js.flow b/src/types/ObjectType.js.flow new file mode 100644 index 0000000..78cc8fb --- /dev/null +++ b/src/types/ObjectType.js.flow @@ -0,0 +1,17 @@ +// @flow + +import Type from './Type' + +import ObjectTypeProperty from './ObjectTypeProperty' + +declare class ObjectType extends Type { + properties: ObjectTypeProperty<$Keys, any>[]; + exact: boolean; + + constructor( + properties?: ObjectTypeProperty<$Keys, any>[], + exact?: boolean + ): void; +} + +export default ObjectType diff --git a/src/types/ObjectTypeProperty.js.flow b/src/types/ObjectTypeProperty.js.flow new file mode 100644 index 0000000..3169f08 --- /dev/null +++ b/src/types/ObjectTypeProperty.js.flow @@ -0,0 +1,27 @@ +// @flow + +import Type from './Type' + +import { type TypeConstraint } from '../typeConstraints' + +declare class ObjectTypeProperty< + K: string | number | symbol, + V +> extends Type { + key: K; + value: Type; + optional: boolean; + constraints: TypeConstraint[]; + __objectType: Type; + + constructor(key: K, value: Type, optional: boolean): void; + + addConstraint(...constraints: TypeConstraint[]): ObjectTypeProperty; + + /** + * Determine whether the property exists on the given input or its prototype chain. + */ + existsOn(input: { [string]: any }): boolean; +} + +export default ObjectTypeProperty diff --git a/src/types/ObjectTypeProperty.ts b/src/types/ObjectTypeProperty.ts index 155d865..79beb22 100644 --- a/src/types/ObjectTypeProperty.ts +++ b/src/types/ObjectTypeProperty.ts @@ -1,5 +1,3 @@ -/* @flow */ - import Type from './Type' import { addConstraints, diff --git a/src/types/RecordType.js.flow b/src/types/RecordType.js.flow new file mode 100644 index 0000000..e821a03 --- /dev/null +++ b/src/types/RecordType.js.flow @@ -0,0 +1,14 @@ +// @flow + +import Type from './Type' + +declare class RecordType extends Type<{ + [K]: V, +}> { + key: Type; + value: Type; + + constructor(key: Type, value: Type): void; +} + +export default RecordType diff --git a/src/types/StringLiteralType.js.flow b/src/types/StringLiteralType.js.flow new file mode 100644 index 0000000..3b400df --- /dev/null +++ b/src/types/StringLiteralType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class StringLiteralType extends Type { + value: T; + + constructor(value: T): void; +} + +export default StringLiteralType diff --git a/src/types/StringType.js.flow b/src/types/StringType.js.flow new file mode 100644 index 0000000..4847bc8 --- /dev/null +++ b/src/types/StringType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class StringType extends Type {} + +export default StringType diff --git a/src/types/SymbolLiteralType.js.flow b/src/types/SymbolLiteralType.js.flow new file mode 100644 index 0000000..e5eea53 --- /dev/null +++ b/src/types/SymbolLiteralType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class SymbolLiteralType extends Type { + value: T; + + constructor(value: T): void; +} + +export default SymbolLiteralType diff --git a/src/types/SymbolLiteralType.ts b/src/types/SymbolLiteralType.ts index d38d48c..b597d78 100644 --- a/src/types/SymbolLiteralType.ts +++ b/src/types/SymbolLiteralType.ts @@ -1,5 +1,3 @@ -/* @flow */ - import Type from './Type' import getErrorMessage from '../getErrorMessage' import Validation, { ErrorTuple, IdentifierPath } from '../Validation' diff --git a/src/types/SymbolType.js.flow b/src/types/SymbolType.js.flow new file mode 100644 index 0000000..c1f137d --- /dev/null +++ b/src/types/SymbolType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class SymbolType extends Type {} + +export default SymbolType diff --git a/src/types/TupleType.js.flow b/src/types/TupleType.js.flow new file mode 100644 index 0000000..6a11bca --- /dev/null +++ b/src/types/TupleType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class TupleType extends Type { + types: $TupleMap(E) => Type>; + + constructor(types: $TupleMap(E) => Type>): void; +} + +export default TupleType diff --git a/src/types/Type.js.flow b/src/types/Type.js.flow new file mode 100644 index 0000000..b42aeb2 --- /dev/null +++ b/src/types/Type.js.flow @@ -0,0 +1,29 @@ +// @flow + +import type Validation from '../Validation' +import type { ErrorTuple, IdentifierPath } from '../Validation' + +/** + * # Type + * + * This is the base class for all types. + */ +declare class Type { + typeName: string; + + errors( + validation: Validation, + path: IdentifierPath, + input: any + ): Generator; + + accepts(input: any): boolean; + + assert(input: any, prefix?: string, path?: IdentifierPath): V; + + validate(input: any, prefix?: string, path?: IdentifierPath): Validation; + + toString(): string; +} + +export default Type diff --git a/src/types/TypeAlias.js.flow b/src/types/TypeAlias.js.flow new file mode 100644 index 0000000..670879f --- /dev/null +++ b/src/types/TypeAlias.js.flow @@ -0,0 +1,18 @@ +// @flow + +import Type from './Type' +import { type TypeConstraint } from '../typeConstraints' + +declare class TypeAlias extends Type { + name: string; + type: Type; + constraints: TypeConstraint[]; + + constructor(name: string, type: Type): void; + + addConstraint(...constraints: TypeConstraint[]): this; + + get hasConstraints(): boolean; +} + +export default TypeAlias diff --git a/src/types/TypeReference.js.flow b/src/types/TypeReference.js.flow new file mode 100644 index 0000000..0b5551a --- /dev/null +++ b/src/types/TypeReference.js.flow @@ -0,0 +1,12 @@ +// @flow + +import Type from './Type' +import TypeAlias from './TypeAlias' + +declare class TypeReference extends Type { + type: () => TypeAlias; + + constructor(type: () => TypeAlias): void; +} + +export default TypeReference diff --git a/src/types/UndefinedLiteralType.js.flow b/src/types/UndefinedLiteralType.js.flow new file mode 100644 index 0000000..464f228 --- /dev/null +++ b/src/types/UndefinedLiteralType.js.flow @@ -0,0 +1,7 @@ +// @flow + +import Type from './Type' + +declare class UndefinedLiteralType extends Type {} + +export default UndefinedLiteralType diff --git a/src/types/UnionType.js.flow b/src/types/UnionType.js.flow new file mode 100644 index 0000000..9e63d4f --- /dev/null +++ b/src/types/UnionType.js.flow @@ -0,0 +1,11 @@ +// @flow + +import Type from './Type' + +declare class UnionType extends Type { + types: Type[]; + + constructor(types: Type[]): void; +} + +export default UnionType diff --git a/tsconfig.json b/tsconfig.json index 3667e9a..217fbeb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["./src"], + "include": ["./src/**/*.ts"], "exclude": ["node_modules", "./src/**/*.spec.ts", "./test"], "compilerOptions": { /* Basic Options */ diff --git a/yarn.lock b/yarn.lock index 94ce0df..9a3a73e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3531,6 +3531,11 @@ flatted@^2.0.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08" integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg== +flow-bin@^0.138.0: + version "0.138.0" + resolved "https://registry.npmjs.org/flow-bin/-/flow-bin-0.138.0.tgz#a0c81a3dd1ed34771b33b7e91078ed260301aa17" + integrity sha512-y3twwNeN0FWEK0vvJo/5SiC/OQVlhubGRyOPIS6p49b2yiiWE/cBFG/aC9kFXFfh7Orewe5O5B2X0+IiEOCYIw== + flush-write-stream@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8"