https://www.tslang.cn/docs/handbook/basic-types.html
https://github.com/sindresorhus/type-fest
https://www.typescriptlang.org/docs/handbook/utility-types.html // ThisType
ReturnType<T>
Awaited<T>
InstanceType
Parameters
Omit, Exclude, Extract, Pick
// Omit<A, keyof B>;
Required 字段全部转成必须,Partial 相反
构造函数类型
closestElement<K extends abstract new (...args: any) => any>(constructor: K): InstanceType<K> | null;
让编辑器有提示:type LiteralType = 'cat' | 'dog' | (string & {})
DeepPartial:https://gist.github.com/navix/6c25c15e0a2d3cd0e5bce999e0086fc9
type ElementOf<T> = T extends (infer E)[] ? E : never
type Json = null | boolean | number | string | Json[] | { [prop: string]: Json }
https://2ality.com/2019/07/testing-static-types.html
https://stackoverflow.com/questions/45251664/typescript-derive-union-type-from-tuple-array-values
const list = ['a', 'b', 'c'] as const; // TS3.4 syntax
type NeededUnionType = typeof list[number]; // 'a'|'b'|'c';
Mapped tuple types:Embed GitHub
// 修改 Key,也可以当作添加 Key
type TransformKey<T, V> = {
[K in keyof T as T[K] extends V ? `on${Capitalize<string & K>}` : never]: T[K];
};
type DataIndex<Data> = Data extends object
? {
[Key in keyof Data]: Key extends string
? Data[Key] extends object
? [Key, ...DataIndex<Data[Key]>] | [Key]
: [Key]
: never
}[keyof Data] | string[]
: never;
// 要严格模式就删除 `| [Key]` 和 `| string[]`
type KebabCase<S extends string> = S extends `${infer Head}${infer Tail}`
? Tail extends Uncapitalize<Tail> // Check if the Tail starts with a lowercase character
? `${Lowercase<Head>}${KebabCase<Tail>}` // If so, just lowercase the Head and recurse
: `${Lowercase<Head>}-${KebabCase<Tail>}` // Otherwise (if Tail starts with uppercase), add a hyphen
: S; // Base case: if no more characters, return the string itself
/**
* Returns tuple types that include every string in union
* TupleUnion<keyof { bar: string; leet: number }>;
* ["bar", "leet"] | ["leet", "bar"];
*/
type TupleUnion<U extends string, R extends string[] = []> = {
[S in U]: Exclude<U, S> extends never ? [...R, S] : TupleUnion<Exclude<U, S>, [...R, S]>;
}[U] & string[];