-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
jsonify.ts
371 lines (288 loc) · 11.9 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* eslint-disable @typescript-eslint/consistent-type-definitions */
// TODO: Convert the `interface`'s to `type`s.
import {expectAssignable, expectNotAssignable, expectType} from 'tsd';
import type {EmptyObject, 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>([] as const);
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);
// TODO: Convert this to a `type`.
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 = new Map<string, number>([['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 NonJsonExtendPrimitiveWithToJSON extends Number {
public fixture = BigInt('42');
public toJSON(): {fixture: string} {
return {
fixture: '42n',
};
}
}
const nonJsonExtendPrimitiveWithToJSON = new NonJsonExtendPrimitiveWithToJSON();
expectNotAssignable<JsonValue>(nonJsonExtendPrimitiveWithToJSON);
expectAssignable<JsonValue>(nonJsonExtendPrimitiveWithToJSON.toJSON());
expectAssignable<Jsonify<NonJsonExtendPrimitiveWithToJSON>>(nonJsonExtendPrimitiveWithToJSON.toJSON());
class NonJsonWithToJSONWrapper {
public inner: NonJsonWithToJSON = nonJsonWithToJSON;
public override = 42;
public toJSON() {
const stringOverride = 'override';
return {
override: stringOverride,
inner: this.inner,
innerDeep: {inner: this.inner},
};
}
}
expectNotAssignable<JsonValue>(new NonJsonWithToJSONWrapper());
type InnerFixture = {fixture: Array<[string, number]>};
expectType<{
override: string;
inner: InnerFixture;
innerDeep: {inner: InnerFixture};
}>({} as Jsonify<NonJsonWithToJSONWrapper>);
class NonJsonWithInvalidToJSON {
public fixture = new Map<string, number>([['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 function_: (_: any) => void;
expectNotAssignable<JsonValue>(function_);
declare const symbol: symbol;
expectNotAssignable<JsonValue>(symbol);
// Plain values fail JSON.stringify()
declare const plainUndefined: Jsonify<typeof undefined>;
expectType<never>(plainUndefined);
declare const plainFunction: Jsonify<typeof function_>;
expectType<never>(plainFunction);
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 arrayMemberUnionWithUndefined: Jsonify<Array<typeof undefined | typeof number>>;
expectType<Array<null | number>>(arrayMemberUnionWithUndefined);
declare const arrayMemberUnionWithUndefinedDeep: Jsonify<Array<Array<typeof undefined | typeof number>> | {foo: Array<typeof undefined | typeof number>}>;
expectType<Array<Array<null | number>> | {foo: Array<null | number>}>(arrayMemberUnionWithUndefinedDeep);
declare const arrayMemberFunction: Jsonify<Array<typeof function_>>;
expectType<null[]>(arrayMemberFunction);
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 objectValueFunction: Jsonify<{keep: string; fn: typeof function_}>;
expectType<{keep: string}>(objectValueFunction);
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);
declare const mixTupleJson: Jsonify<['1', (_: any) => void, 2]>;
expectType<['1', null, 2]>(mixTupleJson);
declare const tupleStringJson: Jsonify<string[] & ['some value']>;
expectType<['some value']>(tupleStringJson);
// 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<EmptyObject>(mapJson);
expectAssignable<Jsonify<typeof map>>({});
// Regression test for https://github.com/sindresorhus/type-fest/issues/466
expectNotAssignable<Jsonify<typeof map>>(42);
expectNotAssignable<Jsonify<typeof map>>({foo: 42});
declare const set: Set<string>;
declare const setJson: Jsonify<typeof set>;
expectType<EmptyObject>(setJson);
expectAssignable<Jsonify<typeof set>>({});
// Regression test for https://github.com/sindresorhus/type-fest/issues/466
expectNotAssignable<Jsonify<typeof set>>(42);
expectNotAssignable<Jsonify<typeof set>>({foo: 42});
// 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.
type OptionalPrimitive = {
a?: string;
};
type OptionalTypeUnion = {
a?: string | (() => any);
};
type NonOptionalTypeUnion = {
a: string | undefined;
};
declare const jsonifiedOptionalPrimitive: Jsonify<OptionalPrimitive>;
declare const jsonifiedOptionalTypeUnion: Jsonify<OptionalTypeUnion>;
declare const jsonifiedNonOptionalTypeUnion: Jsonify<NonOptionalTypeUnion>;
expectType<{a?: string}>(jsonifiedOptionalPrimitive);
expectType<{}>(jsonifiedOptionalTypeUnion);
expectType<{a?: string}>(jsonifiedNonOptionalTypeUnion);
// Test for 'Jsonify support for optional object keys, unserializable object values' #424
// See https://github.com/sindresorhus/type-fest/issues/424
type AppData = {
// Should be kept
requiredString: string;
requiredUnion: number | boolean;
// Should be kept and set to optional
optionalString?: string;
optionalUnion?: number | string;
optionalStringUndefined: string | undefined;
optionalUnionUndefined: number | string | undefined;
// Should be omitted
requiredFunction: () => any;
optionalFunction?: () => any;
requiredFunctionUnion: string | (() => any);
optionalFunctionUnion?: string | (() => any);
optionalFunctionUndefined: (() => any) | undefined;
optionalFunctionUnionUndefined: string | (() => any) | undefined;
};
type ExpectedAppDataJson = {
requiredString: string;
requiredUnion: number | boolean;
optionalString?: string;
optionalUnion?: string | number;
optionalStringUndefined?: string;
optionalUnionUndefined?: string | number;
};
declare const response: Jsonify<AppData>;
expectType<ExpectedAppDataJson>(response);
expectType<any>({} as Jsonify<any>);
declare const objectWithAnyProperty: Jsonify<{
a: any;
}>;
expectType<{a: any}>(objectWithAnyProperty);
declare const objectWithAnyProperties: Jsonify<Record<string, any>>;
expectType<Record<string, any>>(objectWithAnyProperties);
// Test for `Jsonify` support for nested objects with _only_ a name property.
// See https://github.com/sindresorhus/type-fest/issues/657
declare const nestedObjectWithNameProperty: {
first: {
name: string;
};
};
declare const jsonifiedNestedObjectWithNameProperty: Jsonify<
typeof nestedObjectWithNameProperty
>;
expectType<typeof nestedObjectWithNameProperty>(
jsonifiedNestedObjectWithNameProperty,
);
// Regression test for https://github.com/sindresorhus/type-fest/issues/629
declare const readonlyTuple: Jsonify<readonly [1, 2, 3]>;
expectType<[1, 2, 3]>(readonlyTuple);