generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
json.test-d.ts
135 lines (99 loc) · 3.29 KB
/
json.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable @typescript-eslint/consistent-type-definitions */
import { expectAssignable, expectNotAssignable } from 'tsd';
import type { Json } from '../src';
// Valid Json:
expectAssignable<Json>(null);
expectAssignable<Json>(false);
expectAssignable<Json>('');
expectAssignable<Json>(0);
expectAssignable<Json>([]);
expectAssignable<Json>({});
expectAssignable<Json>([0]);
expectAssignable<Json>({ a: 0 });
expectAssignable<Json>({ deeply: [{ nested: 1 }, 'mixed', 'types', 0] });
expectAssignable<Json>(['array', { nested: { mixed: true, types: null } }, 0]);
type JsonCompatibleType = {
c: number;
};
const jsonCompatibleType: JsonCompatibleType = { c: 0 };
expectAssignable<Json>(jsonCompatibleType);
// Invalid Json:
expectNotAssignable<Json>(undefined);
expectNotAssignable<Json>(new Date());
expectNotAssignable<Json>(() => 0);
expectNotAssignable<Json>(new Set());
expectNotAssignable<Json>(new Map());
expectNotAssignable<Json>(Symbol('test'));
expectNotAssignable<Json>({ a: new Date() });
expectNotAssignable<Json>(5 as number | undefined);
interface InterfaceWithOptionalProperty {
a?: number;
}
const interfaceWithOptionalProperty: InterfaceWithOptionalProperty = { a: 0 };
expectNotAssignable<Json>(interfaceWithOptionalProperty);
interface InterfaceWithDate {
a: Date;
}
const interfaceWithDate: InterfaceWithDate = { a: new Date() };
expectNotAssignable<Json>(interfaceWithDate);
interface InterfaceWithOptionalDate {
a?: Date;
}
const interfaceWithOptionalDate: InterfaceWithOptionalDate = { a: new Date() };
expectNotAssignable<Json>(interfaceWithOptionalDate);
interface InterfaceWithUndefinedTypeUnion {
a: number | undefined;
}
const interfaceWithUndefinedTypeUnion: InterfaceWithUndefinedTypeUnion = {
a: 0,
};
expectNotAssignable<Json>(interfaceWithUndefinedTypeUnion);
interface InterfaceWithFunction {
a: () => number;
}
const interfaceWithFunction: InterfaceWithFunction = { a: () => 0 };
expectNotAssignable<Json>(interfaceWithFunction);
type TypeWithDate = {
a: Date;
};
const typeWithDate: TypeWithDate = { a: new Date() };
expectNotAssignable<Json>(typeWithDate);
type TypeWithOptionalDate = {
a?: Date;
};
const typeWithOptionalDate: TypeWithOptionalDate = { a: new Date() };
expectNotAssignable<Json>(typeWithOptionalDate);
type TypeWithUndefinedTypeUnion = {
a: number | undefined;
};
const typeWithUndefinedTypeUnion: TypeWithUndefinedTypeUnion = {
a: 0,
};
expectNotAssignable<Json>(typeWithUndefinedTypeUnion);
type TypeWithFunction = {
a: () => number;
};
const typeWithFunction: TypeWithFunction = { a: () => 0 };
expectNotAssignable<Json>(typeWithFunction);
type TypeWithOptionalProperty = {
a?: number | undefined;
};
const typeWithOptionalProperty: TypeWithOptionalProperty = { a: undefined };
expectNotAssignable<Json>(typeWithOptionalProperty);
// Edge cases:
// The Json type doesn't protect against the `any` type.
expectAssignable<Json>(null as any);
// The Json type gets confused by interfaces. This interface is valid Json,
// but it's incompatible with the Json type.
interface A {
a: number;
}
const a: A = { a: 0 };
expectNotAssignable<Json>(a);
// The Json type gets confused by classes. This class instance is valid Json,
// but it's incompatible with the Json type.
class B {
a!: number;
}
const b = new B();
expectNotAssignable<Json>(b);