Files
element-plus/packages/utils/typescript.ts
一只前端汪 7c16480bcd refactor(style): Update Eslint to V9 and Prettier to V3 (#21949)
* refactor(style): Update Eslint to V9 and Prettier to V3

* fix: vscode color

* fix: vscode color

* chore: remove Useless dependence and old config file

* chore: format

* Merge branch 'dev' into eslintV9

* fix: fix

* fix: ssr test

* fix: ssr test

* fix: use defineConfig

* fix: prettier format and ignore docs dist

* fix: index.mjs => index.js

* fix: Vscode color

* fix: prettier ignore global.d.ts

* fix: format

---------

Co-authored-by: 2586740555 <15972343+CYJ090915@user.noreply.gitee.com>
2025-09-29 14:14:30 +08:00

95 lines
2.4 KiB
TypeScript

export const mutable = <T extends readonly any[] | Record<string, unknown>>(
val: T
) => val as Mutable<typeof val>
export type Mutable<T> = { -readonly [P in keyof T]: T[P] }
export type HTMLElementCustomized<T> = HTMLElement & T
/**
* @deprecated stop to use null
* @see {@link https://github.com/sindresorhus/meta/discussions/7}
*/
export type Nullable<T> = T | null
export type Arrayable<T> = T | T[]
export type Awaitable<T> = Promise<T> | T
type Primitive = null | undefined | string | number | boolean | symbol | bigint
type BrowserNativeObject = Date | FileList | File | Blob | RegExp
/**
* Check whether it is tuple
*
* 检查是否为元组
*
* @example
* IsTuple<[1, 2, 3]> => true
* IsTuple<Array[number]> => false
*/
type IsTuple<T extends ReadonlyArray<any>> = number extends T['length']
? false
: true
/**
* Array method key
*
* 数组方法键
*/
type ArrayMethodKey = keyof any[]
/**
* Tuple index key
*
* 元组下标键
*
* @example
* TupleKey<[1, 2, 3]> => '0' | '1' | '2'
*/
type TupleKey<T extends ReadonlyArray<any>> = Exclude<keyof T, ArrayMethodKey>
/**
* Array index key
*
* 数组下标键
*/
type ArrayKey = number
/**
* Helper type for recursively constructing paths through a type
*
* 用于通过一个类型递归构建路径的辅助类型
*/
type PathImpl<K extends string | number, V> = V extends
| Primitive
| BrowserNativeObject
? `${K}`
: `${K}` | `${K}.${Path<V>}`
/**
* Type which collects all paths through a type
*
* 通过一个类型收集所有路径的类型
*
* @see {@link FieldPath}
*/
type Path<T> =
T extends ReadonlyArray<infer V>
? IsTuple<T> extends true
? {
[K in TupleKey<T>]-?: PathImpl<Exclude<K, symbol>, T[K]>
}[TupleKey<T>] // tuple
: PathImpl<ArrayKey, V> // array
: {
[K in keyof T]-?: PathImpl<Exclude<K, symbol>, T[K]>
}[keyof T] // object
/**
* Type which collects all paths through a type
*
* 通过一个类型收集所有路径的类型
*
* @example
* FieldPath<{ 1: number; a: number; b: string; c: { d: number; e: string }; f: [{ value: string }]; g: { value: string }[]; h: Date; i: FileList; j: File; k: Blob; l: RegExp }> => '1' | 'a' | 'b' | 'c' | 'f' | 'g' | 'c.d' | 'c.e' | 'f.0' | 'f.0.value' | 'g.number' | 'g.number.value' | 'h' | 'i' | 'j' | 'k' | 'l'
*/
export type FieldPath<T> = T extends object ? Path<T> : never