|
| 1 | +import type { JsonValue } from "@std/json/types"; |
| 2 | +import type { IsAny } from "@std/testing/types"; |
| 3 | +export type { IsAny, JsonValue }; |
| 4 | + |
| 5 | +/** |
| 6 | + * Check if a property {@linkcode K} is optional in {@linkcode T}. |
| 7 | + * |
| 8 | + * ```ts |
| 9 | + * import type { Assert } from "@std/testing/types"; |
| 10 | + * |
| 11 | + * type _1 = Assert<IsOptional<{ a?: number }, "a">, true>; |
| 12 | + * type _2 = Assert<IsOptional<{ a?: undefined }, "a">, true>; |
| 13 | + * type _3 = Assert<IsOptional<{ a?: number | undefined }, "a">, true>; |
| 14 | + * type _4 = Assert<IsOptional<{ a: number }, "a">, false>; |
| 15 | + * type _5 = Assert<IsOptional<{ a: undefined }, "a">, false>; |
| 16 | + * type _6 = Assert<IsOptional<{ a: number | undefined }, "a">, false>; |
| 17 | + * ``` |
| 18 | + * @internal |
| 19 | + * |
| 20 | + * @see https://dev.to/zirkelc/typescript-how-to-check-for-optional-properties-3192 |
| 21 | + */ |
| 22 | +export type IsOptional<T, K extends keyof T> = |
| 23 | + Record<PropertyKey, never> extends Pick<T, K> ? true : false; |
| 24 | + |
| 25 | +/** |
| 26 | + * A type that is compatible with JSON. |
| 27 | + * |
| 28 | + * ```ts |
| 29 | + * import type { JsonValue } from "@std/json/types"; |
| 30 | + * import { assertType } from "@std/testing/types"; |
| 31 | + * |
| 32 | + * type IsJsonCompatible<T> = [T] extends [JsonCompatible<T>] ? true : false; |
| 33 | + * |
| 34 | + * assertType<IsJsonCompatible<null>>(true); |
| 35 | + * assertType<IsJsonCompatible<false>>(true); |
| 36 | + * assertType<IsJsonCompatible<0>>(true); |
| 37 | + * assertType<IsJsonCompatible<"">>(true); |
| 38 | + * assertType<IsJsonCompatible<[]>>(true); |
| 39 | + * assertType<IsJsonCompatible<JsonValue>>(true); |
| 40 | + * assertType<IsJsonCompatible<symbol>>(false); |
| 41 | + * // deno-lint-ignore no-explicit-any |
| 42 | + * assertType<IsJsonCompatible<any>>(false); |
| 43 | + * assertType<IsJsonCompatible<unknown>>(false); |
| 44 | + * assertType<IsJsonCompatible<undefined>>(false); |
| 45 | + * // deno-lint-ignore ban-types |
| 46 | + * assertType<IsJsonCompatible<Function>>(false); |
| 47 | + * assertType<IsJsonCompatible<() => void>>(false); |
| 48 | + * assertType<IsJsonCompatible<number | undefined>>(false); |
| 49 | + * assertType<IsJsonCompatible<symbol | undefined>>(false); |
| 50 | + * |
| 51 | + * assertType<IsJsonCompatible<object>>(true); |
| 52 | + * // deno-lint-ignore ban-types |
| 53 | + * assertType<IsJsonCompatible<{}>>(true); |
| 54 | + * assertType<IsJsonCompatible<{ a: 0 }>>(true); |
| 55 | + * assertType<IsJsonCompatible<{ a: "" }>>(true); |
| 56 | + * assertType<IsJsonCompatible<{ a: [] }>>(true); |
| 57 | + * assertType<IsJsonCompatible<{ a: null }>>(true); |
| 58 | + * assertType<IsJsonCompatible<{ a: false }>>(true); |
| 59 | + * assertType<IsJsonCompatible<{ a: boolean }>>(true); |
| 60 | + * assertType<IsJsonCompatible<{ a: Date }>>(false); |
| 61 | + * assertType<IsJsonCompatible<{ a?: Date }>>(false); |
| 62 | + * assertType<IsJsonCompatible<{ a: number }>>(true); |
| 63 | + * assertType<IsJsonCompatible<{ a?: number }>>(true); |
| 64 | + * assertType<IsJsonCompatible<{ a: undefined }>>(false); |
| 65 | + * assertType<IsJsonCompatible<{ a?: undefined }>>(true); |
| 66 | + * assertType<IsJsonCompatible<{ a: number | undefined }>>(false); |
| 67 | + * assertType<IsJsonCompatible<{ a: null }>>(true); |
| 68 | + * assertType<IsJsonCompatible<{ a: null | undefined }>>(false); |
| 69 | + * assertType<IsJsonCompatible<{ a?: null }>>(true); |
| 70 | + * assertType<IsJsonCompatible<{ a: JsonValue }>>(true); |
| 71 | + * // deno-lint-ignore no-explicit-any |
| 72 | + * assertType<IsJsonCompatible<{ a: any }>>(false); |
| 73 | + * assertType<IsJsonCompatible<{ a: unknown }>>(false); |
| 74 | + * // deno-lint-ignore ban-types |
| 75 | + * assertType<IsJsonCompatible<{ a: Function }>>(false); |
| 76 | + * // deno-lint-ignore no-explicit-any |
| 77 | + * assertType<IsJsonCompatible<{ a: () => any }>>(false); |
| 78 | + * // deno-lint-ignore no-explicit-any |
| 79 | + * assertType<IsJsonCompatible<{ a: (() => any) | number }>>(false); |
| 80 | + * // deno-lint-ignore no-explicit-any |
| 81 | + * assertType<IsJsonCompatible<{ a?: () => any }>>(false); |
| 82 | + * class A { |
| 83 | + * a = 34; |
| 84 | + * } |
| 85 | + * assertType<IsJsonCompatible<A>>(true); |
| 86 | + * class B { |
| 87 | + * fn() { |
| 88 | + * return "hello"; |
| 89 | + * }; |
| 90 | + * } |
| 91 | + * assertType<IsJsonCompatible<B>>(false); |
| 92 | + * |
| 93 | + * assertType<IsJsonCompatible<{ a: number } | { a: string }>>(true); |
| 94 | + * assertType<IsJsonCompatible<{ a: number } | { a: () => void }>>(false); |
| 95 | + * |
| 96 | + * assertType<IsJsonCompatible<{ a: { aa: string } }>>(true); |
| 97 | + * interface D { |
| 98 | + * aa: string; |
| 99 | + * } |
| 100 | + * assertType<IsJsonCompatible<D>>(true); |
| 101 | + * interface E { |
| 102 | + * a: D; |
| 103 | + * } |
| 104 | + * assertType<IsJsonCompatible<E>>(true); |
| 105 | + * interface F { |
| 106 | + * _: E; |
| 107 | + * } |
| 108 | + * assertType<IsJsonCompatible<F>>(true); |
| 109 | + * ``` |
| 110 | + * |
| 111 | + * @see This implementation is heavily inspired by https://github.com/microsoft/TypeScript/issues/1897#issuecomment-580962081 . |
| 112 | + */ |
| 113 | +export type JsonCompatible<T> = |
| 114 | + // deno-lint-ignore ban-types |
| 115 | + [Extract<T, Function | symbol | undefined>] extends [never] ? { |
| 116 | + [K in keyof T]: [IsAny<T[K]>] extends [true] ? never |
| 117 | + : T[K] extends JsonValue ? T[K] |
| 118 | + : [IsOptional<T, K>] extends [true] |
| 119 | + ? JsonCompatible<Exclude<T[K], undefined>> | Extract<T[K], undefined> |
| 120 | + : undefined extends T[K] ? never |
| 121 | + : JsonCompatible<T[K]>; |
| 122 | + } |
| 123 | + : never; |
0 commit comments