-
-
Notifications
You must be signed in to change notification settings - Fork 502
/
jsonify.ts
239 lines (185 loc) · 7.65 KB
/
jsonify.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {expectAssignable, expectNotAssignable, expectType} from 'tsd';
import type {Jsonify, JsonValue, NegativeInfinity, PositiveInfinity} from '..';
interface A {
a: number;
}
class B {
a!: number;
}
interface V {
a?: number;
}
interface X {
a: Date;
}
interface Y {
a?: Date;
}
interface Z {
a: number | undefined;
}
interface W {
a?: () => any;
}
declare const a: Jsonify<A>;
declare const b: Jsonify<B>;
declare const v: V; // Not assignable to JsonValue because it is defined as interface
declare const x: X; // Not assignable to JsonValue because it contains Date value
declare const y: Y; // Not assignable to JsonValue because it contains Date value
declare const z: Z; // Not assignable to JsonValue because undefined is not valid Json value
declare const w: W; // Not assignable to JsonValue because a function is not valid Json value
expectAssignable<JsonValue>(null);
expectAssignable<JsonValue>(false);
expectAssignable<JsonValue>(0);
expectAssignable<JsonValue>('');
expectAssignable<JsonValue>([]);
expectAssignable<JsonValue>({});
expectAssignable<JsonValue>([0]);
expectAssignable<JsonValue>({a: 0});
expectAssignable<JsonValue>(a);
expectAssignable<JsonValue>(b);
expectAssignable<JsonValue>({a: {b: true, c: {}}, d: [{}, 2, 'hi']});
expectAssignable<JsonValue>([{}, {a: 'hi'}, null, 3]);
expectNotAssignable<JsonValue>(new Date());
expectNotAssignable<JsonValue>([new Date()]);
expectNotAssignable<JsonValue>({a: new Date()});
expectNotAssignable<JsonValue>(v);
expectNotAssignable<JsonValue>(x);
expectNotAssignable<JsonValue>(y);
expectNotAssignable<JsonValue>(z);
expectNotAssignable<JsonValue>(w);
expectNotAssignable<JsonValue>(undefined);
expectNotAssignable<JsonValue>(5 as number | undefined);
interface Geometry {
type: 'Point' | 'Polygon';
coordinates: [number, number];
}
const point: Geometry = {
type: 'Point',
coordinates: [1, 1],
};
expectNotAssignable<JsonValue>(point);
expectAssignable<Jsonify<Geometry>>(point);
// The following const values are examples of values `v` that are not JSON, but are *jsonable* using
// `v.toJSON()` or `JSON.parse(JSON.stringify(v))`
declare const dateToJSON: Jsonify<Date>;
expectAssignable<string>(dateToJSON);
expectAssignable<JsonValue>(dateToJSON);
// The following commented `= JSON.parse(JSON.stringify(x))` is an example of how `parsedStringifiedX` could be created.
// * Note that this would be an unsafe assignment because `JSON.parse()` returns type `any`.
// But by inspection `JSON.stringify(x)` will use `x.a.toJSON()`. So the `JSON.parse()` result can be
// assigned to `Jsonify<X>` if the `@typescript-eslint/no-unsafe-assignment` ESLint rule is ignored
// or an `as Jsonify<X>` is added.
// * This test is not about how `parsedStringifiedX` is created, but about its type, so the `const` value is declared.
declare const parsedStringifiedX: Jsonify<X>; // = JSON.parse(JSON.stringify(x));
expectAssignable<JsonValue>(parsedStringifiedX);
expectAssignable<string>(parsedStringifiedX.a);
class NonJsonWithToJSON {
public fixture: Map<string, number> = new Map([['a', 1], ['b', 2]]);
public toJSON(): {fixture: Array<[string, number]>} {
return {
fixture: [...this.fixture.entries()],
};
}
}
const nonJsonWithToJSON = new NonJsonWithToJSON();
expectNotAssignable<JsonValue>(nonJsonWithToJSON);
expectAssignable<JsonValue>(nonJsonWithToJSON.toJSON());
expectAssignable<Jsonify<NonJsonWithToJSON>>(nonJsonWithToJSON.toJSON());
class NonJsonWithInvalidToJSON {
public fixture: Map<string, number> = new Map([['a', 1], ['b', 2]]);
// This is intentionally invalid `.toJSON()`.
// It is invalid because the result is not assignable to `JsonValue`.
public toJSON(): {fixture: Map<string, number>} {
return {
fixture: this.fixture,
};
}
}
const nonJsonWithInvalidToJSON = new NonJsonWithInvalidToJSON();
expectNotAssignable<JsonValue>(nonJsonWithInvalidToJSON);
expectNotAssignable<JsonValue>(nonJsonWithInvalidToJSON.toJSON());
// Not jsonable types; these types behave differently when used as plain values, as members of arrays and as values of objects
declare const undefined: undefined;
expectNotAssignable<JsonValue>(undefined);
declare const fn: (_: any) => void;
expectNotAssignable<JsonValue>(fn);
declare const symbol: symbol;
expectNotAssignable<JsonValue>(symbol);
// Plain values fail JSON.stringify()
declare const plainUndefined: Jsonify<typeof undefined>;
expectType<never>(plainUndefined);
declare const plainFn: Jsonify<typeof fn>;
expectType<never>(plainFn);
declare const plainSymbol: Jsonify<typeof symbol>;
expectType<never>(plainSymbol);
// Array members become null
declare const arrayMemberUndefined: Jsonify<Array<typeof undefined>>;
expectType<null[]>(arrayMemberUndefined);
declare const arrayMemberFn: Jsonify<Array<typeof fn>>;
expectType<null[]>(arrayMemberFn);
declare const arrayMemberSymbol: Jsonify<Array<typeof symbol>>;
expectType<null[]>(arrayMemberSymbol);
// When used in object values, these keys are filtered
declare const objectValueUndefined: Jsonify<{keep: string; undefined: typeof undefined}>;
expectType<{keep: string}>(objectValueUndefined);
declare const objectValueFn: Jsonify<{keep: string; fn: typeof fn}>;
expectType<{keep: string}>(objectValueFn);
declare const objectValueSymbol: Jsonify<{keep: string; symbol: typeof symbol}>;
expectType<{keep: string}>(objectValueSymbol);
// Symbol keys are filtered
declare const objectKeySymbol: Jsonify<{[key: typeof symbol]: number; keep: string}>;
expectType<{keep: string}>(objectKeySymbol);
// Number, String and Boolean values are turned into primitive counterparts
declare const number: Number;
expectNotAssignable<JsonValue>(number);
declare const string: String;
expectNotAssignable<JsonValue>(string);
declare const boolean: Boolean;
expectNotAssignable<JsonValue>(boolean);
declare const numberJson: Jsonify<typeof number>;
expectType<number>(numberJson);
declare const stringJson: Jsonify<typeof string>;
expectType<string>(stringJson);
declare const booleanJson: Jsonify<typeof boolean>;
expectType<boolean>(booleanJson);
declare const tupleJson: Jsonify<[string, Date]>;
expectType<[string, string]>(tupleJson);
declare const tupleRestJson: Jsonify<[string, ...Date[]]>;
expectType<[string, ...string[]]>(tupleRestJson);
// BigInt fails JSON.stringify
declare const bigInt: Jsonify<bigint>;
expectType<never>(bigInt);
declare const int8Array: Int8Array;
declare const int8ArrayJson: Jsonify<typeof int8Array>;
expectType<Record<string, number>>(int8ArrayJson);
declare const map: Map<string, number>;
declare const mapJson: Jsonify<typeof map>;
expectType<{}>(mapJson);
declare const set: Set<string>;
declare const setJson: Jsonify<typeof set>;
expectType<{}>(setJson);
// Positive and negative Infinity, NaN and null are turned into null
// NOTE: NaN is not detectable in TypeScript, so it is not tested; see https://github.com/sindresorhus/type-fest/issues/406
declare const positiveInfinity: PositiveInfinity;
declare const positiveInfJson: Jsonify<typeof positiveInfinity>;
expectType<null>(positiveInfJson);
declare const negativeInf: NegativeInfinity;
declare const negativeInfJson: Jsonify<typeof negativeInf>;
expectType<null>(negativeInfJson);
// Test that optional type members are not discarded wholesale.
interface OptionalPrimitive {
a?: string;
}
interface OptionalTypeUnion {
a?: string | (() => any);
}
interface NonOptionalTypeUnion {
a: string | undefined;
}
declare const jsonifiedOptionalPrimitive: Jsonify<OptionalPrimitive>;
declare const jsonifiedOptionalTypeUnion: Jsonify<OptionalTypeUnion>;
declare const jsonifiedNonOptionalTypeUnion: Jsonify<NonOptionalTypeUnion>;
expectType<{a?: string}>(jsonifiedOptionalPrimitive);
expectType<{a?: never}>(jsonifiedOptionalTypeUnion);
expectType<{a: never}>(jsonifiedNonOptionalTypeUnion);