TS试炼-3
摘要:
本项目意在于让你更好的了解 TS 的类型系统,编写你自己的类型工具,或者只是单纯的享受挑战的乐趣!我们同时希望可以建立一个社区,在这里你可以提出你在实际环境中遇到的问题,或者帮助他人解答疑惑 - 这些问题也可能被选中成为题库的一部分!
1、排除最后一项
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00016-medium-pop/README.zh-CN.md
type Pop<T extends unknown[]> = T extends [...infer V, infer _] ? V : []
type Pop<T extends unknown[]> = T extends [...infer V, unknown] ? V : []
2、Promise.all
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00020-medium-promise-all/README.zh-CN.md
declare function PromiseAll<T extends unknown[]>(values: [...T]): Promise<{
[P in keyof T]: Awaited<T[P]>
}>
declare function PromiseAll<T extends unknown[]>(values: [...T]): Promise<{
[P in keyof T]: T[P] extends Promise<infer V> | infer V ? V : T[P]
}>
3、查找类型
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00062-medium-type-lookup/README.zh-CN.md
type LookUp<U extends {type: string}, T extends U['type']> = U extends { type: T } ? U : never
4、去除空白
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00106-medium-trimleft/README.zh-CN.md
type Space = ' ' | '\t' | '\n'
type TrimLeft<S extends string> = S extends `${Space}${infer V}` ? TrimLeft<V> : S
type TrimRight<S extends string> = S extends `${infer V}${Space}` ? TrimRight<V> : S
// https://github.com/type-challenges/type-challenges/blob/main/questions/00106-medium-trimleft/README.zh-CN.md
type Trim<S extends string> = TrimRight<TrimLeft<S>>
type Trim<S extends string> = S extends `${Space}${infer V}` | `${infer V}${Space}` ? Trim<V> : S
5、Capitalize 首字母大小
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00110-medium-capitalize/README.zh-CN.md
type MyCapitalize<S extends string> = S extends `${infer F}${infer V}` ? `${Uppercase<F>}${V}` : ''
6、Replace 替换
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00116-medium-replace/README.zh-CN.md
type Replace<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer R}` ? `${F}${To}${R}` : S
// https://github.com/type-challenges/type-challenges/blob/main/questions/00119-medium-replaceall/README.zh-CN.md
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer R}` ? `${F}${To}${ReplaceAll<R, From, To>}` : S
7、追加参数
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00191-medium-append-argument/README.zh-CN.md
type AppendArgument<Fn extends (...args: any) => any, A> = Fn extends (...args: infer T) => infer V ? (...args: [...T, A]) => V : never
8、Permutation(联合类型的全排列)
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00296-medium-permutation/README.zh-CN.md
type Permutation<T, U = T> = [T] extends [never] ? [] : T extends U ? [T, ...Permutation<Exclude<U, T>>] : [];
type Permutation<T, U = T> = [T] extends [never] ? [] : T extends any ? [T, ...Permutation<Exclude<U, T>>] : [];
9、计算字符串长度
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00298-medium-length-of-string/README.zh-CN.md
type LengthOfString<S extends string, T extends string[] = []> = S extends `${infer F}${infer R}` ? LengthOfString<R, [...T, F]> : T['length']
10、Flatten 扁平数组
ts
// https://github.com/type-challenges/type-challenges/blob/main/questions/00459-medium-flatten/README.zh-CN.md
type Flatten<T extends unknown[], U extends unknown[] = []> = T extends [infer F, ...infer R]
? F extends unknown[]
? Flatten<[...F, ...R], U>
: Flatten<R, [...U, F]>
: U;
type Flatten<T extends unknown[]> = T extends [infer F, ...infer R]
? F extends unknown[]
? [...Flatten<F>, ...Flatten<R>]
: [F, ...Flatten<R>]
: T;
评论
0条评论
暂无内容,去看看其他的吧~