November 2019
Beginner
804 pages
20h 1m
English
Here's a first example showing how to create a composite validator type:
import * as t from "io-ts";
...
const countryValidator = t.type({
id: t.string,
name: t.string,
capitalCity: t.string
});
Here, we have used t.type to define a runtime type that combines different properties.
Now let's look at a more concrete example:
import * as t from "io-ts"; import {ThrowReporter} from "io-ts/lib/ThrowReporter"; const countryValidator = t.type({ id: t.string, name: t.string, capitalCity: t.string, }); // extract the corresponding static types interface Country extends t.TypeOf<typeof countryValidator> {} const validCountry:Country = { id: "BE", name: "Belgium", capitalCity: "Brussels" }; const invalidCountry: unknown = { foo: "foo", ...Read now
Unlock full access