/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import type { JsonDeserialized } from "./jsonDeserialized.js"; import type { JsonSerializable, JsonSerializableOptions } from "./jsonSerializable.js"; import type { OpaqueJsonDeserialized, OpaqueJsonSerializable } from "./opaqueJson.js"; /** * Helper to return an Opaque Json type version of Json type * * @remarks * To use this helper, create a helper function that filters type `T` through at * least {@link JsonSerializable} and optionally {@link JsonDeserialized}. Then * cast value through `unknown as JsonTypeToOpaqueJson`, where * `Options` reflects the serialization capabilities of that area. * * @example * ```ts * function castToOpaqueJson(value: JsonSerializable): JsonTypeToOpaqueJson { * return value as unknown as JsonTypeToOpaqueJson; * } * ``` * * @internal */ export type JsonTypeToOpaqueJson< T, Options extends JsonSerializableOptions = { AllowExactly: []; AllowExtensionOf: never; }, > = T extends JsonSerializable & JsonDeserialized ? OpaqueJsonSerializable< T, Options extends { AllowExactly: unknown[] } ? Options["AllowExactly"] : [], Options extends { AllowExtensionOf: unknown } ? Options["AllowExtensionOf"] : never > & OpaqueJsonDeserialized< T, Options extends { AllowExactly: unknown[] } ? Options["AllowExactly"] : [], Options extends { AllowExtensionOf: unknown } ? Options["AllowExtensionOf"] : never > : T extends JsonDeserialized ? OpaqueJsonDeserialized< T, Options extends { AllowExactly: unknown[] } ? Options["AllowExactly"] : [], Options extends { AllowExtensionOf: unknown } ? Options["AllowExtensionOf"] : never > : T extends JsonSerializable ? OpaqueJsonSerializable< T, Options extends { AllowExactly: unknown[] } ? Options["AllowExactly"] : [], Options extends { AllowExtensionOf: unknown } ? Options["AllowExtensionOf"] : never > : never; /** * Helper to extract Json type from an Opaque Json type * * @remarks * This type only works with basic serialization capabilities (options). * Attempts to make `Options` generic resulted in infinite recursion * in TypeScript compiler that was not understood, so this type only * supports TJson (value type) variance. * * To use this helper, create a helper function that accepts * `OpaqueJsonSerializable | OpaqueJsonDeserialized`. * * @example * ```ts * function exposeFromOpaqueJson | OpaqueJsonDeserialized>( * opaque: TOpaque, * ): OpaqueJsonToJsonType { * return opaque as unknown as OpaqueJsonToJsonType; * } * ``` * * @internal */ export type OpaqueJsonToJsonType< TOpaque extends OpaqueJsonSerializable | OpaqueJsonDeserialized, > = TOpaque extends OpaqueJsonSerializable & OpaqueJsonDeserialized ? JsonSerializable & JsonDeserialized : TOpaque extends OpaqueJsonDeserialized ? JsonDeserialized : TOpaque extends OpaqueJsonSerializable ? JsonSerializable : never;