@fgv/ts-json-base 5.0.0-29 → 5.0.0-30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -209,11 +209,44 @@ declare const jsonArray_2: Validator<JsonArray, IJsonValidatorContext>;
209
209
 
210
210
  /**
211
211
  * A constrained type that is compatible with JSON serialization.
212
+ *
213
+ * This type transforms input types to ensure they can be safely serialized to JSON:
214
+ * - JSON primitives (string, number, boolean, null) are preserved as-is
215
+ * - `undefined` is allowed for TypeScript compatibility with optional properties
216
+ * - Objects are recursively transformed with all properties made JSON-compatible
217
+ * - Arrays are transformed to contain only JSON-compatible elements
218
+ * - Functions are transformed to error types
219
+ * - Other non-JSON types are transformed to error types
220
+ *
221
+ * Note: While `undefined` is technically not JSON-serializable, it's allowed here
222
+ * to support TypeScript's optional property patterns. Use `sanitizeJsonObject`
223
+ * to remove undefined properties before actual JSON serialization.
224
+ *
212
225
  * @param T - The type to be constrained
213
226
  * @returns A constrained type that is compatible with JSON serialization.
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * interface IUser {
231
+ * name: string;
232
+ * email?: string; // Optional property can be undefined
233
+ * }
234
+ *
235
+ * type UserCompatible = JsonCompatible<IUser>; // Allows undefined for email
236
+ *
237
+ * const user: UserCompatible = {
238
+ * name: "John",
239
+ * email: undefined // This works
240
+ * };
241
+ *
242
+ * // Before JSON serialization, sanitize to remove undefined:
243
+ * const sanitized = sanitizeJsonObject(user); // Removes undefined properties
244
+ * JSON.stringify(sanitized.value); // Safe to serialize
245
+ * ```
246
+ *
214
247
  * @public
215
248
  */
216
- export declare type JsonCompatible<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
249
+ export declare type JsonCompatible<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive | undefined ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
217
250
  [K in keyof T]: JsonCompatible<T[K]>;
218
251
  } : ['Error: Non-JSON type'];
219
252
 
@@ -35,11 +35,44 @@ export type JsonValueType = 'primitive' | 'object' | 'array';
35
35
  type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
36
36
  /**
37
37
  * A constrained type that is compatible with JSON serialization.
38
+ *
39
+ * This type transforms input types to ensure they can be safely serialized to JSON:
40
+ * - JSON primitives (string, number, boolean, null) are preserved as-is
41
+ * - `undefined` is allowed for TypeScript compatibility with optional properties
42
+ * - Objects are recursively transformed with all properties made JSON-compatible
43
+ * - Arrays are transformed to contain only JSON-compatible elements
44
+ * - Functions are transformed to error types
45
+ * - Other non-JSON types are transformed to error types
46
+ *
47
+ * Note: While `undefined` is technically not JSON-serializable, it's allowed here
48
+ * to support TypeScript's optional property patterns. Use `sanitizeJsonObject`
49
+ * to remove undefined properties before actual JSON serialization.
50
+ *
38
51
  * @param T - The type to be constrained
39
52
  * @returns A constrained type that is compatible with JSON serialization.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * interface IUser {
57
+ * name: string;
58
+ * email?: string; // Optional property can be undefined
59
+ * }
60
+ *
61
+ * type UserCompatible = JsonCompatible<IUser>; // Allows undefined for email
62
+ *
63
+ * const user: UserCompatible = {
64
+ * name: "John",
65
+ * email: undefined // This works
66
+ * };
67
+ *
68
+ * // Before JSON serialization, sanitize to remove undefined:
69
+ * const sanitized = sanitizeJsonObject(user); // Removes undefined properties
70
+ * JSON.stringify(sanitized.value); // Safe to serialize
71
+ * ```
72
+ *
40
73
  * @public
41
74
  */
42
- export type JsonCompatible<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
75
+ export type JsonCompatible<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive | undefined ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
43
76
  [K in keyof T]: JsonCompatible<T[K]>;
44
77
  } : ['Error: Non-JSON type'];
45
78
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "5.0.0-29",
3
+ "version": "5.0.0-30",
4
4
  "description": "Typescript types and basic functions for working with json",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-json-base.d.ts",
@@ -39,12 +39,12 @@
39
39
  "@rushstack/eslint-patch": "~1.12.0",
40
40
  "@rushstack/eslint-config": "4.4.0",
41
41
  "eslint-plugin-tsdoc": "~0.4.0",
42
- "@fgv/ts-utils": "5.0.0-29",
43
- "@fgv/ts-extras": "5.0.0-29",
44
- "@fgv/ts-utils-jest": "5.0.0-29"
42
+ "@fgv/ts-utils": "5.0.0-30",
43
+ "@fgv/ts-utils-jest": "5.0.0-30",
44
+ "@fgv/ts-extras": "5.0.0-30"
45
45
  },
46
46
  "peerDependencies": {
47
- "@fgv/ts-utils": "5.0.0-29"
47
+ "@fgv/ts-utils": "5.0.0-30"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "heft test --clean",