-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertions-as-type-guards.ts
69 lines (59 loc) 路 1.31 KB
/
assertions-as-type-guards.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
68
69
import {expectType} from 'tsd';
import test from '../../entrypoints/main.mjs';
type Expected = {foo: 'bar'};
const expected: Expected = {foo: 'bar'};
test('assert', t => {
const actual = expected as Expected | undefined;
if (t.truthy(actual)) {
expectType<Expected>(actual);
} else {
expectType<undefined>(actual);
}
});
test('deepEqual', t => {
const actual: unknown = {};
if (t.deepEqual(actual, expected)) {
expectType<Expected>(actual);
}
});
test('like', t => {
const actual: unknown = {};
if (t.like(actual, expected)) {
expectType<Expected>(actual);
}
});
test('is', t => {
const actual: unknown = 2;
if (t.is(actual, 3 as const)) {
expectType<3>(actual);
}
});
test('false', t => {
const actual: unknown = true;
if (t.false(actual)) {
expectType<false>(actual);
}
});
test('falsy', t => {
type Actual = Expected | undefined | false | 0 | '' | 0n;
const actual = undefined as Actual;
if (t.falsy(actual)) {
expectType<Exclude<Actual, Expected>>(actual);
} else {
expectType<Expected>(actual);
}
});
test('true', t => {
const actual: unknown = false;
if (t.true(actual)) {
expectType<true>(actual);
}
});
test('truthy', t => {
const actual = expected as Expected | undefined;
if (t.truthy(actual)) {
expectType<Expected>(actual);
} else {
expectType<undefined>(actual);
}
});