Skip to content

Commit

Permalink
remove maybe combinator
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 4, 2017
1 parent c0bff96 commit 81cb096
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 70 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This package exports two default reporters
Example

```js
import { PathReporter, ThrowReporter } from '../src/reporters/default'
import { PathReporter, ThrowReporter } from 'io-ts/reporters/default'

const validation = t.validate({"name":"Giulio"}, Person)

Expand Down Expand Up @@ -146,6 +146,26 @@ import * as t from 'io-ts'
| keyof | `keyof M` | `t.keyof(M)` |
| recursive types | | `t.recursion(name, definition)` |

# Custom combinators

You can define your own combinators. Let's see some interesting examples

## The `maybe` combinator

```ts
export function maybe<RT extends t.Any>(type: RT, name?: string): t.UnionType<[RT, typeof t.null], t.TypeOf<RT> | null> {
return t.union([type, t.null], name)
}
```

## The `brand` combinator

```ts
export function brand<T, B extends string>(type: t.Type<T>, brand: B): t.Type<T & { readonly __brand: B }> {
return type as any
}
```

# Known issues

Due to an upstream [bug](https://github.com/Microsoft/TypeScript/issues/14041), VS Code might display weird types for nested interfaces
Expand Down
18 changes: 0 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,6 @@ export function recursion<T>(name: string, definition: (self: Any) => Any): Type
return Result
}

//
// maybes
//

export class MaybeType<RT extends Any> extends Type<TypeOf<RT> | null> {
constructor(name: string, validate: Validate<TypeOf<RT> | null>, public readonly type: RT) {
super(name, validate)
}
}

export function maybe<RT extends Any>(type: RT, name?: string): MaybeType<RT> {
return new MaybeType(
name || `(${getTypeName(type)} | null)`,
(v, c) => v === null ? success(v) : type.validate(v, c),
type
)
}

//
// arrays
//
Expand Down
32 changes: 0 additions & 32 deletions test/maybe.ts

This file was deleted.

12 changes: 5 additions & 7 deletions test/recursion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('recursion', () => {
it('should succeed validating a valid value', () => {
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
b: t.union([self, t.undefined, t.null])
}))
assertSuccess(t.validate({ a: 1, b: null }, T))
assertSuccess(t.validate({ a: 1, b: { a: 2, b: null } }, T))
Expand All @@ -19,7 +19,7 @@ describe('recursion', () => {
it('should return the same reference if validation succeeded', () => {
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
b: t.union([self, t.undefined, t.null])
}))
const value = { a: 1, b: { a: 2, b: null } }
assertStrictEqual(t.validate(value, T), value)
Expand All @@ -28,18 +28,16 @@ describe('recursion', () => {
it('should fail validating an invalid value', () => {
const T = t.recursion('T', self => t.interface({
a: t.number,
b: t.maybe(self)
b: t.union([self, t.undefined, t.null])
}))
assertFailure(t.validate(1, T), [
'Invalid value 1 supplied to : T'
])
assertFailure(t.validate({}, T), [
'Invalid value undefined supplied to : T/a: number',
'Invalid value undefined supplied to : T/b: (T | null)'
'Invalid value undefined supplied to : T/a: number'
])
assertFailure(t.validate({ a: 1, b: {} }, T), [
'Invalid value undefined supplied to : T/b: (T | null)/a: number',
'Invalid value undefined supplied to : T/b: (T | null)/b: (T | null)'
'Invalid value {} supplied to : T/b: (T | undefined | null)'
])
})

Expand Down
12 changes: 0 additions & 12 deletions typings-checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ const R1 = t.refinement(t.number, n => n % 2 === 0)
;('s' as TypeOf<typeof R1>)
;(2 as TypeOf<typeof R1>)

//
// maybes
//

const M1 = t.maybe(t.number)
// $ExpectError Type 'string' cannot be converted to type 'number | null'
;('s' as TypeOf<typeof M1>)
// $ExpectError Type 'undefined' cannot be converted to type 'number | null'
;(undefined as TypeOf<typeof M1>)
;(2 as TypeOf<typeof M1>)
;(null as TypeOf<typeof M1>)

//
// arrays
//
Expand Down

0 comments on commit 81cb096

Please sign in to comment.