/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ /** * Type constraint for types that are likely serializable as JSON or have a custom * alternate type. * * @remarks * Use `JsonableTypeWith` for just JSON serializable types. * See {@link Jsonable} for serialization pitfalls. * * @privateRemarks * Prefer using `Jsonable` over this type that is an implementation detail. * @legacy @beta */ export type JsonableTypeWith = undefined | null | boolean | number | string | T | Internal_InterfaceOfJsonableTypesWith | ArrayLike>; /** * @remarks * This type is a kludge and not intended for general use. * * @privateRemarks * Internal type testing for compatibility uses TypeOnly filter which cannot handle recursive "pure" types. * This interface along with ArrayLike above avoids pure type recursion issues, but introduces a limitation on * the ability of {@link Jsonable} to detect array-like types that are not handled naively ({@link JSON.stringify}). * The TypeOnly filter is not useful for {@link JsonableTypeWith}; so, if type testing improves, this can be removed. * @legacy @beta */ export interface Internal_InterfaceOfJsonableTypesWith { [index: string | number]: JsonableTypeWith; } /** * Used to constrain a type `T` to types that are serializable as JSON. * Produces a compile-time error if `T` contains non-Jsonable members. * * @remarks * Note that this does NOT prevent using of values with non-json compatible data, * it only prevents using values with types that include non-json compatible data. * This means that one can, for example, pass in a value typed with json compatible * interface into this function, * that could actually be a class with lots on non-json compatible fields and methods. * * Important: `T extends Jsonable` is incorrect (does not even compile). * * The optional 'TReplaced' parameter may be used to permit additional leaf types to support * situations where a `replacer` is used to handle special values (e.g., `Jsonable<{ x: IFluidHandle }, IFluidHandle>`). * * Note that `Jsonable` does not protect against the following pitfalls when serializing with JSON.stringify(): * * - `undefined` properties on objects are omitted (i.e., properties become undefined instead of equal to undefined). * * - When `undefined` appears as the root object or as an array element it is coerced to `null`. * * - Non-finite numbers (`NaN`, `+/-Infinity`) are also coerced to `null`. * * - prototypes and non-enumerable properties are lost. * * - `ArrayLike` types that are not arrays and are serialized as `{ length: number }`. * * Also, `Jsonable` does not prevent the construction of circular references. * * Using `Jsonable` or `Jsonable` is a type alias for * {@link JsonableTypeWith}`` and should not be used if precise type safety is desired. * * @example Typical usage * * ```typescript * function foo(value: Jsonable) { ... } * ``` * @legacy @beta */ export type Jsonable = boolean extends (T extends never ? true : false) ? JsonableTypeWith : unknown extends T ? JsonableTypeWith : T extends undefined | null | boolean | number | string | TReplaced ? T : Extract extends never ? T extends object ? T extends (infer U)[] ? Jsonable[] : { [K in keyof T]: Extract extends never ? Jsonable : never; } : never : never; //# sourceMappingURL=jsonable.d.ts.map