From defba7e4cc67fdd31b6ebe3fc1060a45490d90cd Mon Sep 17 00:00:00 2001 From: Ivan Babak Date: Mon, 10 Jul 2017 01:58:59 -0700 Subject: [PATCH] README fixes and tweaks (#60) * README fixes and tweaks * Added a link to the "TypeScript integration" section. * Reordered arrays, dictionaries to be grouped together. * Removed `maybe`, it's no longer supplied (since 0.2.2). * Added `interface inheritance`. * Added a link to the "TypeScript integration" section to the recursive types in the table (there is an example for `recursion`). * Reordered `refinement` to the end, closer to other types unsupported by TypeScript. * Added explanation what is `maybe` in its example. Refs https://github.com/gcanti/io-ts/issues/59 * README heading for recursive types --- README.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c10e2f09d..1f938b807 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ export const string: t.Type = { } ``` -Note: The `_A` field contains a dummy value and is useful to extract a static type from the runtime type (see the section "TypeScript integration" below) +Note: The `_A` field contains a dummy value and is useful to extract a static type from the runtime type (see the ["TypeScript integration"](#typescript-integration) section below) A runtime type can be used to validate an object in memory (for example an API payload) @@ -107,6 +107,8 @@ type IPerson = { } ``` +## Recursive types + Note that recursive types can't be inferred ```js @@ -140,23 +142,23 @@ import * as t from 'io-ts' | any | `any` | `t.any` | | never | `never` | `t.never` | | integer | ✘ | `t.Integer` | -| generic array | `Array` | `t.Array` | -| generic dictionary | `{ [key: string]: any }` | `t.Dictionary` | +| array of any | `Array` | `t.Array` | +| array of type | `Array` | `t.array(A)` | +| dictionary of any | `{ [key: string]: any }` | `t.Dictionary` | +| dictionary of type | `{ [key: A]: B }` | `t.dictionary(A, B)` | | function | `Function` | `t.Function` | -| arrays | `Array` | `t.array(A)` | | literal | `'s'` | `t.literal('s')` | -| maybe | `A | null` | `t.maybe(A)` | | partial | `Partial<{ name: string }>` | `t.partial({ name: t.string })` | | readonly | `Readonly<{ name: string }>` | `t.readonly({ name: t.string })` | | readonly array | `ReadonlyArray` | `t.readonlyArray(t.number)` | -| dictionaries | `{ [key: A]: B }` | `t.dictionary(A, B)` | -| refinement | ✘ | `t.refinement(A, predicate)` | -| interface | `{ name: string }` | `t.interface({ name: t.string })` | -| tuple | `[A, B]` | `t.tuple([A, B])` | -| union | `A \| B` | `t.union([A, B])` | -| intersection | `A & B` | `t.intersection([A, B])` | +| interface | `interface A { name: string }` | `t.interface({ 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 ])` | +| intersection | `A & B` | `t.intersection([ A, B ])` | | keyof | `keyof M` | `t.keyof(M)` | -| recursive types | | `t.recursion(name, definition)` | +| recursive types | see [Recursive types](#recursive-types) | `t.recursion(name, definition)` | +| refinement | ✘ | `t.refinement(A, predicate)` | | map | ✘ | `t.map(f, type)` | | prism | ✘ | `t.prism(type, getOption)` | @@ -219,6 +221,8 @@ You can define your own combinators. Let's see some examples ## The `maybe` combinator +An equivalent to `T | null` + ```ts export function maybe( type: RT,