source: frontend/node_modules/ts-interface-checker/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.6 KB
Line 
1# ts-interface-checker
2
3[![Build Status](https://travis-ci.org/gristlabs/ts-interface-checker.svg?branch=master)](https://travis-ci.org/gristlabs/ts-interface-checker)
4[![npm version](https://badge.fury.io/js/ts-interface-checker.svg)](https://badge.fury.io/js/ts-interface-checker)
5
6
7> Runtime library to validate data against TypeScript interfaces.
8
9This package is the runtime support for validators created by
10[ts-interface-builder](https://github.com/gristlabs/ts-interface-builder).
11It allows validating data, such as parsed JSON objects received
12over the network, or parsed JSON or YAML files, to check if they satisfy a
13TypeScript interface, and to produce informative error messages if they do not.
14
15## Installation
16
17```bash
18npm install --save-dev ts-interface-builder
19npm install --save ts-interface-checker
20```
21
22## Usage
23
24Suppose you have a TypeScript file defining an interface:
25```typescript
26// foo.ts
27interface Square {
28 size: number;
29 color?: string;
30}
31```
32
33The first step is to generate some code for runtime checks:
34```bash
35`npm bin`/ts-interface-builder foo.ts
36```
37
38It produces a file like this:
39```typescript
40// foo-ti.js
41import * as t from "ts-interface-checker";
42
43export const Square = t.iface([], {
44 "size": "number",
45 "color": t.opt("string"),
46});
47...
48```
49
50Now at runtime, to check if a value satisfies the Square interface:
51```typescript
52import fooTI from "./foo-ti";
53import {createCheckers} from "ts-interface-checker";
54
55const {Square} = createCheckers(fooTI);
56
57Square.check({size: 1}); // OK
58Square.check({size: 1, color: "green"}); // OK
59Square.check({color: "green"}); // Fails with "value.size is missing"
60Square.check({size: 4, color: 5}); // Fails with "value.color is not a string"
61```
62
63Note that `ts-interface-builder` is only needed for the build-time step, and
64`ts-interface-checker` is needed at runtime. That's why the recommendation is to npm-install the
65former using `--save-dev` flag and the latter using `--save`.
66
67## Checking method calls
68
69If you have an interface with methods, you can validate method call arguments and return values:
70```typescript
71// greet.ts
72interface Greeter {
73 greet(name: string): string;
74}
75```
76
77After generating the runtime code, you can now check calls like:
78```typescript
79import greetTI from "./greet-ti";
80import {createCheckers} from "ts-interface-checker";
81
82const {Greeter} = createCheckers(greetTI);
83
84Greeter.methodArgs("greet").check(["Bob"]); // OK
85Greeter.methodArgs("greet").check([17]); // Fails with "value.name is not a string"
86Greeter.methodArgs("greet").check([]); // Fails with "value.name is missing"
87
88Greeter.methodResult("greet").check("hello"); // OK
89Greeter.methodResult("greet").check(null); // Fails with "value is not a string"
90```
91
92## Type suites
93
94If one type refers to a type defined in another file, you need to tell the interface checker about
95all type names when you call `createCheckers()`. E.g. given
96
97```typescript
98// color.ts
99export type Color = RGB | string;
100export type RGB = [number, number, number];
101```
102
103```typescript
104// shape.ts
105import {Color} from "./color";
106export interface Square {
107 size: number;
108 color?: Color;
109}
110```
111
112the produced files `color-ti.ts` and `shape-ti.ts` do not automatically refer to each other, but
113expect you to relate them in `createCheckers()` call:
114```typescript
115import color from "./color-ti";
116import shape from "./shape-ti";
117import {createCheckers} from "ts-interface-checker";
118
119const {Square} = createCheckers(shape, color); // Pass in all required type suites.
120
121Square.check({size: 1, color: [255,255,255]});
122```
123
124## Strict checking
125
126You may check that data contains no extra properties. Note that it is not generally recommended as
127it this prevents backward compatibility: if you add new properties to an interface, then older
128code with strict checks will not accept them.
129
130Following on the example above:
131```typescript
132Square.strictCheck({size: 1, color: [255,255,255], bg: "blue"}); // Fails with value.bg is extraneous
133Square.strictCheck({size: 1, color: [255,255,255,0.5]}); // Fails with ...value.color[3] is extraneous
134```
135
136## Type guards
137
138Standard `Checker` objects do the type checking logic, but are unable to make the TypeScript
139compiler aware that an object of `unknown` type implements a certain interface.
140
141Basic code:
142```typescript
143const unk: unknown = {size: 1, color: "green"};
144// Type is unknown, so TypeScript will not let you access the members.
145console.log(unk.size); // Error: "Object is of type 'unknown'"
146```
147
148With a `Checker` available:
149```typescript
150import fooTI from "./foo-ti";
151import {createCheckers} from "ts-interface-checker";
152
153const {Square} = createCheckers(fooTI);
154
155const unk: unknown = {size: 1, color: "green"};
156
157if (Square.test(unk)) {
158 // unk does implement Square, but TypeScript is not aware of it.
159 console.log(unk.size); // Error: "Object is of type 'unknown'"
160}
161```
162
163To enable type guard functionality on the existing `test`, and `strictTest` functions, `Checker`
164objects should be cast to `CheckerT<>` using the appropriate type.
165
166Using `CheckerT<>`:
167```typescript
168import {Square} from "./foo";
169import fooTI from "./foo-ti";
170import {createCheckers, CheckerT} from "ts-interface-checker";
171
172const {Square} = createCheckers(fooTI) as {Square: CheckerT<Square>};
173
174const unk: unknown = {size: 1, color: "green"};
175
176if (Square.test(unk)) {
177 // TypeScript is now aware that unk implements Square, and allows member access.
178 console.log(unk.size);
179}
180```
181
182## Type assertions
183
184`CheckerT<>` will eventually support type assertions using the `check` and `strictCheck` functions,
185however, this feature is not yet fully working in TypeScript.
Note: See TracBrowser for help on using the repository browser.