/** * Union类型转换为Intersection类型 * * ```ts * import type { UnionToIntersection } from 'sunny-js' * type Obj = UnionToIntersection<{ a: 1 } | { b: 2 }> * // type Obj = {a: 1} & {b: 2} * ``` * * @see https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type * @category Utility Types */ export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; /** * 与keyof相反,取得对象的值类型 * * ```ts * import type { ValueOf } from 'sunny-js' * const objA = { key: 'value' } as const * type ObjA = ValueOf * // type ObjA = "value" * const objB = { key: 'value' } * type ObjB = ValueOf * // type ObjB = string * ``` * * @see https://stackoverflow.com/questions/49285864/is-there-a-valueof-similar-to-keyof-in-typescript * @category Utility Types */ export type ValueOf = O[keyof O]; /** * 创建一个对象类型,但属性都是可选的 * * ```ts * import type { PartialRecord } from 'sunny-js' * const obj1: PartialRecord<'key1' | 'key2', any> = { * key1: 'value1', * } * // No type error * * const obj2: Record<'key1' | 'key2', any> = { * key1: 'value1', * } * // TS2741: Property 'key2' is missing in type '{ key1: string; }' but required in type 'Record"key1" | "key2", any>'. * ``` * * @see {@link Record} * @category Utility Types */ export type PartialRecord = { [P in K]?: T; }; /** * 去除readonly modifier * * ```ts * import type { DeepWriteable } from 'sunny-js' * type ObjA = DeepWriteable<{ path: readonly string[] | string }>['path'] * // type ObjA = string[] | string * type ArrA = DeepWriteable * // type ArrA = string[] | string * ``` * * @see {@link Readonly} * @category Utility Types */ export type DeepWriteable = { -readonly [P in keyof T]: DeepWriteable; };