Skip to content

Commit

Permalink
add aliases for null and interface, closes #63 (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Aug 31, 2017
1 parent 11ef277 commit e5c0998
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

# 0.6.2

- **New Feature**
- add aliases for `null` and `interface`, closes #63 (@gcanti)

# 0.6.1

- **Internal**
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import * as t from 'io-ts'

| Type | TypeScript annotation syntax | Runtime type / combinator |
|------|-------|-------------|
| null | `null` | `t.null` |
| null | `null` | `t.null` or `t.nullType` |
| undefined | `undefined` | `t.undefined` |
| string | `string` | `t.string` |
| number | `number` | `t.number` |
Expand All @@ -151,7 +151,7 @@ import * as t from 'io-ts'
| partial | `Partial<{ name: string }>` | `t.partial({ name: t.string })` |
| readonly | `Readonly<{ name: string }>` | `t.readonly({ name: t.string })` |
| readonly array | `ReadonlyArray<number>` | `t.readonlyArray(t.number)` |
| interface | `interface A { name: string }` | `t.interface({ name: t.string })` |
| interface | `interface A { name: string }` | `t.interface({ name: t.string })` or `t.type({ name: t.string })` |
| interface inheritance | `interface B extends A {}` | `t.intersection([ A, t.interface({}) ])` |
| tuple | `[ A, B ]` | `t.tuple([ A, B ])` |
| union | `A \| B` | `t.union([ A, B ])` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "0.6.1",
"version": "0.6.2",
"description": "TypeScript compatible runtime type system for IO validation",
"files": ["lib"],
"main": "lib/index.js",
Expand Down
20 changes: 8 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export interface NullType extends Type<null> {
readonly _tag: 'NullType'
}

const nullType: NullType = {
/** An alias of `null` */
export const nullType: NullType = {
_A,
_tag: 'NullType',
name: 'null',
Expand Down Expand Up @@ -358,7 +359,8 @@ export interface InterfaceType<P extends Props> extends Type<InterfaceOf<P>> {
readonly props: P
}

export function _interface<P extends Props>(props: P, name?: string): InterfaceType<P> {
/** An alias of `interface` */
export function type<P extends Props>(props: P, name?: string): InterfaceType<P> {
return {
_A,
_tag: 'InterfaceType',
Expand Down Expand Up @@ -407,12 +409,12 @@ export function partial<P extends Props>(props: P, name?: string): PartialType<P
for (let k in props) {
partials[k] = union([props[k], undefinedType])
}
const type = _interface(partials)
const partial = type(partials)
return {
_A,
_tag: 'PartialType',
name: name || type.name,
validate: (v, c) => type.validate(v, c) as any,
name: name || partial.name,
validate: (v, c) => partial.validate(v, c) as any,
props: (partials as any) as PartialPropsOf<P>
}
}
Expand Down Expand Up @@ -651,10 +653,4 @@ export function readonlyArray<RT extends Any>(type: RT, name?: string): Readonly
}
}

export {
nullType as null,
undefinedType as undefined,
arrayType as Array,
functionType as Function,
_interface as interface
}
export { nullType as null, undefinedType as undefined, arrayType as Array, functionType as Function, type as interface }
7 changes: 7 additions & 0 deletions test/default-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ describe('Integer', () => {
assertFailure(t.validate('foo', t.Integer), ['Invalid value "foo" supplied to : Integer'])
})
})

describe('null', () => {
it('should support the alias `nullType`', () => {
assertSuccess(t.validate(null, t.null))
assertFailure(t.validate(1, t.null), ['Invalid value 1 supplied to : null'])
})
})
5 changes: 5 additions & 0 deletions test/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ describe('interface', () => {
assertFailure(t.validate({}, T), ['Invalid value undefined supplied to : { a: string }/a: string'])
assertFailure(t.validate({ a: 1 }, T), ['Invalid value 1 supplied to : { a: string }/a: string'])
})

it('should support the alias `type`', () => {
const T = t.type({ a: t.string })
assertSuccess(t.validate({ a: 's' }, T))
})
})

0 comments on commit e5c0998

Please sign in to comment.