1
1
import { Either , Left , Right , isRight } from 'fp-ts/lib/Either'
2
- import { Predicate , identity } from 'fp-ts/lib/function'
2
+ import { Predicate } from 'fp-ts/lib/function'
3
3
4
4
declare global {
5
5
interface Array < T > {
@@ -27,6 +27,11 @@ export type Any = Type<any, any>
27
27
export type TypeOf < RT extends Any > = RT [ '_A' ]
28
28
export type InputOf < RT extends Any > = RT [ '_S' ]
29
29
30
+ /**
31
+ * Laws:
32
+ * 1. validate(x).fold(() => x, serialize) = x
33
+ * 2. validate(serialize(x)) = Right(x)
34
+ */
30
35
export class Type < S , A > {
31
36
readonly _A : A
32
37
readonly _S : S
@@ -46,13 +51,15 @@ export class Type<S, A> {
46
51
}
47
52
}
48
53
54
+ export const identity = < A > ( a : A ) : A => a
55
+
49
56
export const getFunctionName = ( f : any ) : string => f . displayName || f . name || `<function${ f . length } >`
50
57
51
58
export const getContextEntry = ( key : string , type : Any | NeverType ) : ContextEntry => ( { key, type } )
52
59
53
60
export const getValidationError = ( value : any , context : Context ) : ValidationError => ( { value, context } )
54
61
55
- const pushAll = < A > ( xs : Array < A > , ys : Array < A > ) : void => Array . prototype . push . apply ( xs , ys )
62
+ export const getDefaultContext = ( type : Any ) : Context => [ { key : '' , type } ]
56
63
57
64
export const failures = < T > ( errors : Errors ) : Validation < T > => new Left ( errors )
58
65
@@ -61,11 +68,11 @@ export const failure = <T>(value: any, context: Context): Validation<T> =>
61
68
62
69
export const success = < T > ( value : T ) : Validation < T > => new Right < Errors , T > ( value )
63
70
64
- const getDefaultContext = ( type : Any ) : Context => [ { key : '' , type } ]
65
-
66
71
export const validate = < S , A > ( value : S , type : Type < S , A > ) : Validation < A > =>
67
72
type . validate ( value , getDefaultContext ( type ) )
68
73
74
+ const pushAll = < A > ( xs : Array < A > , ys : Array < A > ) : void => Array . prototype . push . apply ( xs , ys )
75
+
69
76
//
70
77
// basic types
71
78
//
@@ -77,7 +84,7 @@ export class NullType extends Type<any, null> {
77
84
}
78
85
}
79
86
80
- /** An alias of `null` */
87
+ /** @ alias `null` */
81
88
export const nullType : NullType = new NullType ( )
82
89
83
90
export class UndefinedType extends Type < any , undefined > {
@@ -339,7 +346,6 @@ export const array = <RT extends Any>(type: RT, name?: string): ArrayType<RT> =>
339
346
340
347
export type Props = { [ key : string ] : Any }
341
348
342
- // TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
343
349
export type InterfaceOf < P extends Props > = { [ K in keyof P ] : TypeOf < P [ K ] > }
344
350
345
351
const getNameFromProps = ( props : Props ) : string =>
@@ -402,16 +408,14 @@ export class InterfaceType<P extends Props> extends Type<any, InterfaceOf<P>> {
402
408
}
403
409
}
404
410
405
- /** An alias of `interface` */
411
+ /** @ alias `interface` */
406
412
export const type = < P extends Props > ( props : P , name ?: string ) : InterfaceType < P > => new InterfaceType ( props , name )
407
413
408
414
//
409
415
// partials
410
416
//
411
417
412
- // TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
413
418
export type PartialOf < P extends Props > = { [ K in keyof P ] ?: TypeOf < P [ K ] > }
414
- // TODO remove this once https://github.com/Microsoft/TypeScript/issues/14041 is fixed
415
419
export type PartialPropsOf < P extends Props > = { [ K in keyof P ] : UnionType < [ P [ K ] , UndefinedType ] > }
416
420
417
421
export class PartialType < P extends Props > extends Type < any , PartialOf < P > > {
@@ -731,11 +735,4 @@ export function strict<P extends Props>(props: P, name?: string): StrictType<P>
731
735
return new StrictType ( props , name )
732
736
}
733
737
734
- export {
735
- identity ,
736
- nullType as null ,
737
- undefinedType as undefined ,
738
- arrayType as Array ,
739
- functionType as Function ,
740
- type as interface
741
- }
738
+ export { nullType as null , undefinedType as undefined , arrayType as Array , functionType as Function , type as interface }
0 commit comments