types.ts
This file
Path
src/utils/types.ts
116 lines · 3.9 KiB · TypeScript
Last changed by
chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
4.9.0Yusuke Wadabefore adoption82bdc4d
lines 1–7why, one step back1/**2 * @module3 * Types utility.4 */56/* eslint-disable @typescript-eslint/no-explicit-any */7export type Expect<T extends true> = Tchore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 8–9why, one step back8export type Equal<X, Y> =9 (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false4.9.0Yusuke Wadabefore adoption82bdc4d
lines 10–17why, one step back10export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true1112export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (13 k: infer I14) => void15 ? I16 : never17chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 18–19why, one step back18export type RemoveBlankRecord<T> =19 T extends Record<infer K, unknown> ? (K extends string ? T : never) : never4.9.0Yusuke Wadabefore adoption82bdc4d
lines 20–39why, one step back2021export type IfAnyThenEmptyObject<T> = 0 extends 1 & T ? {} : T2223export type JSONPrimitive = string | boolean | number | null24export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]25export type JSONObject = {26 [key: string]: JSONPrimitive | JSONArray | JSONObject | object | InvalidJSONValue27}28export type InvalidJSONValue = undefined | symbol | ((...args: unknown[]) => unknown)2930type InvalidToNull<T> = T extends InvalidJSONValue ? null : T3132type IsInvalid<T> = T extends InvalidJSONValue ? true : false3334/**35 * symbol keys are omitted through `JSON.stringify`36 */37type OmitSymbolKeys<T> = { [K in keyof T as K extends symbol ? never : K]: T[K] }3839export type JSONValue = JSONObject | JSONArray | JSONPrimitivefix(types): JSONParsed infer unknown values (#4405)Jonathan Hainesbefore adoptionf8c9b6e
lines 40–52why, one step back40/**41 * Convert a type to a JSON-compatible type.42 *43 * Non-JSON values such as `Date` implement `.toJSON()`,44 * so they can be transformed to a value assignable to `JSONObject`45 *46 * `JSON.stringify()` throws a `TypeError` when it encounters a `bigint` value,47 * unless a custom `replacer` function or `.toJSON()` method is provided.48 *49 * This behaviour can be controlled by the `TError` generic type parameter,50 * which defaults to `bigint | ReadonlyArray<bigint>`.51 * You can set it to `never` to disable this check.52 */fix(types): fix 4.9.8 regression (#4448)Aadit Olkarbefore adoption4f5a305
lines 53–55why, one step back53export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends {54 toJSON(): infer J55}4.9.0Yusuke Wadabefore adoption82bdc4d
lines 56–58why, one step back56 ? (() => J) extends () => JSONPrimitive57 ? J58 : (() => J) extends () => { toJSON(): unknown }chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 59–60why, one step back59 ? {}60 : JSONParsed<J, TError>4.9.0Yusuke Wadabefore adoption82bdc4d
line 61why, one step back61 : T extends JSONPrimitivechore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 62–83why, one step back62 ? T63 : T extends InvalidJSONValue64 ? never65 : T extends ReadonlyArray<unknown>66 ? { [K in keyof T]: JSONParsed<InvalidToNull<T[K]>, TError> }67 : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never>68 ? {}69 : T extends object70 ? T[keyof T] extends TError71 ? never72 : {73 [K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true74 ? never75 : K]: boolean extends IsInvalid<T[K]>76 ? JSONParsed<T[K], TError> | undefined77 : JSONParsed<T[K], TError>78 }79 : T extends unknown80 ? T extends TError81 ? never82 : JSONValue83 : never4.9.0Yusuke Wadabefore adoption82bdc4d
lines 84–106why, one step back8485/**86 * Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.87 * @copyright from sindresorhus/type-fest88 */89export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {}9091/**92 * A simple extension of Simplify that will deeply traverse array elements.93 */94export type SimplifyDeepArray<T> = T extends any[]95 ? { [E in keyof T]: SimplifyDeepArray<T[E]> }96 : Simplify<T>9798export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }99100export type RequiredKeysOf<BaseType extends object> = Exclude<101 {102 [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never103 }[keyof BaseType],104 undefined105>106chore: bump linter and formatter (#4568)ryubefore adoptionb6f4bcd
lines 107–108why, one step back107export type HasRequiredKeys<BaseType extends object> =108 RequiredKeysOf<BaseType> extends never ? false : true4.9.0Yusuke Wadabefore adoption82bdc4d
lines 109–116why, one step back109110export type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false111112/**113 * String literal types with auto-completion114 * @see https://github.com/Microsoft/TypeScript/issues/29729115 */116export type StringLiteralUnion<T> = T | (string & Record<never, never>)