import { Simplify, WithoutIndexSignature } from '../../types'; /** * @link https://github.com/remix-run/remix/blob/2248669ed59fd716e267ea41df5d665d4781f4a9/packages/remix-server-runtime/serialize.ts */ type JsonPrimitive = boolean | number | string | null; type NonJsonPrimitive = Function | symbol | undefined; type IsAny = 0 extends T & 1 ? true : false; type JsonReturnable = JsonPrimitive | undefined; type IsRecord = keyof WithoutIndexSignature extends never ? true : false; export type Serialize = IsAny extends true ? any : unknown extends T ? unknown : T extends JsonReturnable ? T : T extends Map | Set ? object : T extends NonJsonPrimitive ? never : T extends { toJSON(): infer U; } ? U : T extends [] ? [] : T extends [unknown, ...unknown[]] ? SerializeTuple : T extends readonly (infer U)[] ? (U extends NonJsonPrimitive ? null : Serialize)[] : T extends object ? IsRecord extends true ? Record> : Simplify>> : never; /** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */ type SerializeTuple = { [K in keyof T]: T[K] extends NonJsonPrimitive ? null : Serialize; }; type SerializeObjectKey, K> = K extends symbol ? never : IsAny extends true ? K : unknown extends T[K] ? K : T[K] extends NonJsonPrimitive ? never : K; /** * JSON serialize objects (not including arrays) and classes * @internal **/ export type SerializeObject = { [K in keyof T as SerializeObjectKey]: Serialize; }; /** * Extract keys from T where the value dosen't extend undefined * Note: Can't parse IndexSignature or Record types */ type FilterDefinedKeys = Exclude<{ [K in keyof T]: undefined extends T[K] ? never : K; }[keyof T], undefined>; /** * Get value of exactOptionalPropertyTypes config */ type ExactOptionalPropertyTypes = { a?: 0 | undefined; } extends { a?: 0; } ? false : true; /** * Check if T has an index signature */ type HasIndexSignature = string extends keyof T ? true : false; /** * { [key: string]: number | undefined } --> { [key: string]: number } */ type HandleIndexSignature = { [K in keyof Omit>]: Exclude; }; /** * { a: number | undefined } --> { a?: number } * Note: Can't parse IndexSignature or Record types */ type HandleUndefined = { [K in keyof Omit>]?: Exclude; }; /** * Handle undefined, index signature and records */ type UndefinedToOptional = Pick, FilterDefinedKeys>> & (ExactOptionalPropertyTypes extends true ? HandleIndexSignature & HandleUndefined> : HasIndexSignature extends true ? HandleIndexSignature : HandleUndefined); export {}; //# sourceMappingURL=serialize.d.ts.map