Skip to content
Myr

types.ts

This file

Path

src/utils/types.ts

120 lines · 4.2 KiB · TypeScript

Last changed by

chore: replace prettier with oxfmt (#5435)Yusuke Wadabefore adoption8dcd52b

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–48why, one step back
1/**
2 * @module3 * Types utility.4 */56/* eslint-disable @typescript-eslint/no-explicit-any */7export type Expect<T extends true> = T8export type Equal<X, Y> =9 (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false10export 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 : never1718export type RemoveBlankRecord<T> =19 T extends Record<infer K, unknown> ? (K extends string ? T : never) : never2021export 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 | JSONPrimitive40/**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 *

docs: fix typos in code comments and link third-party middleware section (#5343)Aidanbefore adoptione7b38ee

line 49why, one step back
49 * This behavior can be controlled by the `TError` generic type parameter,

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 50–65why, one step back
50 * which defaults to `bigint | ReadonlyArray<bigint>`.
51 * You can set it to `never` to disable this check.52 */53export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends {54 toJSON(): infer J55}56 ? (() => J) extends () => JSONPrimitive57 ? J58 : (() => J) extends () => { toJSON(): unknown }59 ? {}60 : JSONParsed<J, TError>61 : T extends JSONPrimitive62 ? T63 : T extends InvalidJSONValue64 ? never65 : T extends ReadonlyArray<unknown>

fix(types): strip extra properties from array types in JSONParsed (#5103)arman-luthrabefore adoptionab590e4

lines 66–70why, one step back
66 ? { [K in keyof T]: JSONParsed<InvalidToNull<T[K]>, TError> } extends infer A
67 ? A extends ReadonlyArray<unknown>68 ? A // plain array or tuple; the mapped type preserves array-ness69 : JSONParsed<InvalidToNull<T[number]>, TError>[] // array with extra properties; JSON.stringify drops them70 : never

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 71–76why, one step back
71 : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never>
72 ? {}73 : T extends object74 ? T[keyof T] extends TError75 ? never76 : {

chore: replace prettier with oxfmt (#5435)Yusuke Wadabefore adoption8dcd52b

lines 77–79why, one step back
77 [
78 K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true ? never : K79 ]: boolean extends IsInvalid<T[K]>

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 80–120why, one step back
80 ? JSONParsed<T[K], TError> | undefined
81 : JSONParsed<T[K], TError>82 }83 : T extends unknown84 ? T extends TError85 ? never86 : JSONValue87 : never8889/**90 * 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.91 * @copyright from sindresorhus/type-fest92 */93export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {}9495/**96 * A simple extension of Simplify that will deeply traverse array elements.97 */98export type SimplifyDeepArray<T> = T extends any[]99 ? { [E in keyof T]: SimplifyDeepArray<T[E]> }100 : Simplify<T>101102export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }103104export type RequiredKeysOf<BaseType extends object> = Exclude<105 {106 [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never107 }[keyof BaseType],108 undefined109>110111export type HasRequiredKeys<BaseType extends object> =112 RequiredKeysOf<BaseType> extends never ? false : true113114export type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false115116/**117 * String literal types with auto-completion118 * @see https://github.com/Microsoft/TypeScript/issues/29729119 */120export type StringLiteralUnion<T> = T | (string & Record<never, never>)