自定义工具类型
common types
Nullable<Type>
ts
type Nullable<T> = T | null;
FuncStrict
ts
type FuncStrict = (...args: any[]) => any;
string types
CapitalizeStrict<Str>
严格的首字母大写(除了首字母大写,其余均为小写)
ts
type CapitalizeStrict<Str extends string> = Capitalize<Lowercase<Str>>;
示例
ts
type CapitalizeStrictTest = Str.CapitalizeStrict<'zeng'>; // 'Zeng'
type CapitalizeStrictTest2 = Str.CapitalizeStrict<'yOmuKi'>; // 'Yomuki'
SeparatorCase2CamelCase<Str,Separator>
分隔符转小驼峰
ts
/**
* 任意分隔符转小驼峰
*/
type SeparatorCase2CamelCase<Str extends string, Separator extends string> = Str extends `${infer Head}${Separator}${infer Rest}`
? `${Head}${Capitalize<SeparatorCase2CamelCase<Rest, Separator>>}`
: Str;
/**
* 下划线转小驼峰
*/
type SnakeCase2CamelCase<Str extends string> = SeparatorCase2CamelCase<Str, '_'>;
/**
* 中划线转小驼峰
*/
type KebabCase2CamelCase<Str extends string> = SeparatorCase2CamelCase<Str, '-'>;
示例
ts
type SnakeCase2CamelCaseTest = Str.SnakeCase2CamelCase<'zeng_shuai_aBb'>; // 'zengShuaiABb'
type SnakeCase2CamelCaseTest2 = Str.SnakeCase2CamelCase<'Akb'>; // 'Akb'
type SnakeCase2CamelCaseTest3 = Str.SnakeCase2CamelCase<'a_1b'>; // 'a1b'
type KebabCase2CamelCaseTest = Str.KebabCase2CamelCase<'zeng-shuai-aBb'>; // 'zengShuaiABb'
智能驼峰,自动识别-
、_
、,并转换为小驼峰
ts
/**
* 将字符串数组转为首字母大写的字符串
*/
type CapitalizeStrictArray2String<Words extends string[]> = Words extends [infer First, ...infer Rest]
? `${CapitalizeStrict<First>}${CapitalizeStrictArray2String<Rest>}`
: '';
/**
* 将字符串数组转为小驼峰
*/
type CamelCaseArray2String<Words extends string[]> = Words extends [infer First, ...infer Rest]
? `${First}${CapitalizeStrictArray2String<Rest>}`
: '';
/**
* 驼峰分隔符
*/
type CamelCaseSeparator = '-' | '_' | ' ';
/**
* 小驼峰
*/
type CamelCase<Str extends string> = CamelCaseArray2String<Spilt<Str, CamelCaseSeparator>>;
示例
ts
expectType<Str.CamelCase<'zeng_yomuki'>>('zengYomuki')
expectType<Str.CamelCase<'zeng-yomuki'>>('zengYomuki')
expectType<Str.CamelCase<'zeng yomuki'>>('zengYomuki')
expectType<Str.CamelCase<'zeng yoMuki'>>('zengYomuki')
Include<Str,Search>
判断字符串是否包含某个子串
ts
type _Include<Str extends string, Search extends string> = Str extends `${infer _R1}${Search}${infer _R2}` ? true : false;
type Include<Str extends string, Search extends string> = Str extends '' ? (Search extends '' ? true : false) : _Include<Str, Search>;
TrimLeft<Str>
去除字符串左边的空格
ts
type TrimLeft<Str extends string> = Str extends ` ${infer R}` ? TrimLeft<R> : Str;
TrimRight<Str>
去除字符串右边的空格
ts
type TrimRight<Str extends string> = Str extends `${infer R} ` ? TrimRight<R> : Str;
Trim<Str>
去除字符串两边的空格
ts
type Trim<Str extends string> = TrimLeft<TrimRight<Str>>;
StartsWith<Str,Search>
判断字符串是否以某个子串开头
ts
type StartsWith<Str extends string, Search extends string> = Str extends `${Search}${infer _R}` ? true : false;
EndsWith<Str,Search>
判断字符串是否以某个子串结尾
ts
type EndsWith<Str extends string, Search extends string> = Str extends `${infer _R}${Search}` ? true : false;
Replace<Str,Search,Replace>
替换字符串中的某个子串
ts
type Replace<Str extends string, Search extends string, Replace extends string> = Str extends `${infer R1}${Search}${infer R2}`
? `${R1}${Replace}${R2}`
: Str;
ReplaceAll<Str,Search,Replace>
替换字符串中的所有某个子串
ts
type ReplaceAll<Str extends string, Search extends string, Replace extends string> = Str extends `${infer R1}${Search}${infer R2}`
? ReplaceAll<`${R1}${Replace}${R2}`, Search, Replace>
: Str;
Split<Str,Separator>
分割字符串
ts
type Spilt<Str extends string, Separator extends string> = Str extends `${infer Head}${Separator}${infer Tail}`
? [Head, ...Spilt<Tail, Separator>]
: Str extends Separator
? []
: [Str];
示例
ts
type SpiltTest = Str.Spilt<'Zeng Shuai Yomuki', ' '>; // ['Zeng', 'Shuai', 'Yomuki']
type SpiltTest2 = Str.Spilt<'a', 'a'>; // ['', '']
type SpiltTest3 = Str.Spilt<'yomuki', ''>; // ['y', 'o', 'm', 'u', 'k', 'i']
Length<Str>
获取字符串长度
ts
type Length<Str extends string> = Spilt<Str, ''>['length'];
array types
Join<Arr,Separator>
数组转字符串
ts
type Join<Arr extends Array[string | number], Separator extends string> = Arr extends [infer Head, ...infer Tail]
? Tail extends []
? Head
: `${Head}${Separator}${Join<Tail, Separator>}`
: '';
示例
ts
type JoinTest = Arr.Join<['Zeng', 'Shuai', 'Yomuki'], ' '>; // 'Zeng Shuai Yomuki'
type JoinTest2 = Arr.Join<['Zeng', 'Shuai', 'Yomuki'], ''>; // 'ZengShuaiYomuki'
type JoinTest3 = Arr.Join<[''], ''>; // ''
object types
CamelCasedProperties<T>
将对象的所有属性转为小驼峰
ts
type CamelCasedProperties<T extends ObjectStrict> = {
[K in keyof T as Str.CamelCase<string & K>]: T[K] extends object ? CamelCasedProperties<T[K]> : T[K];
};
示例
ts
type CamelCasedPropertiesTest = Obj.CamelCasedProperties<{
foo_bar: string
foo_bar_baz: string
person: {
first_name: string
}
}>;
expectType<CamelCasedPropertiesTest>({
fooBar: '',
fooBarBaz: '',
person: {
firstName: ''
}
})
ExpectedKeysByType<T,ValueType>
获取对象中值类型为某个类型的所有键
ts
type ExpectedKeysByType<T extends object, ValueType> = {
[K in keyof T]-?: StrictConditional<ValueType, T[K], K, never, never>;
}[keyof T];
示例
ts
type ExpectedKeysByTypeTest = Obj.ExpectedKeysByType<
{
a: string
b: number
c: boolean
d: () => void
e?: string
f: 1 | 2 | 3
},
string
>;
expectType<ExpectedKeysByTypeTest>('a')
FilteredKeysByType<T,ValueType>
获取对象中值类型不为某个类型的所有键
ts
type FilteredKeysByType<T extends object, ValueType> = {
[K in keyof T]-?: StrictConditional<ValueType, T[K], never, K, K>;
}[keyof T];
示例
ts
type FilteredKeysByTypeTest = Obj.FilteredKeysByType<
{
a: string
b: number
c: boolean
d: () => void
e?: string
f: 1 | 2 | 3
},
string
>; // "b" | "c" | "d" | "e" | "f"
expectType<FilteredKeysByTypeTest>('b')
PickByValueType<T,ValueType>
选择指定类型的属性创建新对象
ts
type PickByValueType<T extends object, ValueType> = Pick<T, ExpectedKeysByType<T, ValueType>>;
示例
ts
type PickByValueTypeTest = Obj.PickByValueType<
{
a: string
b: number
c: boolean
d: () => void
e?: string
f: 1 | 2 | 3
},
string
>;
expectType<PickByValueTypeTest>({ a: 'a' })
OmitByValueType<T,ValueType>
剔除指定类型的属性,返回新对象
ts
type OmitByValueType<T extends object, ValueType> = Pick<T, FilteredKeysByType<T, ValueType>>;
示例
ts
type OmitByValueTypeTest = Obj.OmitByValueType<
{
a: string
b: number
c: boolean
d: () => void
e?: string
f: 1 | 2 | 3
},
1 | 2 | 3
>;
expectType<OmitByValueTypeTest>({ a: 'xx', b: 1, c: true, d: () => {} })
function types
FirstArgument<T>
获取函数的第一个参数类型
ts
type FirstArgument<T extends FuncStrict> = T extends (arg: infer P, ...args: any[]) => any ? P : never;
LastArgument<T>
获取函数的最后一个参数类型
ts
type LastArgument<T extends FuncStrict> = T extends (arg: infer P) => any
? P
: T extends (...args: infer R) => any
? R extends [...any, infer Q]
? Q
: never
: never;