-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test-d.ts
67 lines (56 loc) · 1.19 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { expectType } from 'tsd';
import {
checks,
ensures,
requires,
isDefined,
error,
asserts,
unreachable,
} from './index';
// CONTRACTS
function requiresExample(value: string | null) {
requires(isDefined(value));
expectType<string>(value);
}
function checksExample(value: string | null) {
checks(isDefined(value));
expectType<string>(value);
}
function ensuresExample(value: string | null) {
ensures(isDefined(value));
expectType<string>(value);
}
function assertsExample(value: string | null) {
asserts(isDefined(value));
expectType<string>(value);
}
// UTILS
function errorExample(value: string | null) {
const result = value ?? error();
expectType<string>(result);
}
interface Named {
name: string;
}
function isNamed(value: any): value is Named {
return value != null && typeof value.name === 'string';
}
function useIfTypeGuardExample(value: any) {
const withName = isNamed(value) ? value : error();
expectType<Named>(withName);
}
function exhaustiveSwitch(foo: 'a' | 'b'): void {
let x;
switch (foo) {
case 'a':
x = 0;
break;
case 'b':
x = 1;
break;
default:
unreachable(foo);
}
expectType<number>(x);
}