手写常见的typescript类型

文章展示了TypeScript中的一些实用泛型操作,如Pick、Readonly、TupleToObject、FirstOfArray、LengthOfTuple、Exclude、PickPartial、Awaited、If、Concat、Includes、Push和Unshift,这些都是在处理类型时增强代码灵活性和安全性的工具。
摘要由CSDN通过智能技术生成

1.Pick

type myPick<T,K extends keyof T> = { [key in K]:T[key] }

2.Readonly

type myReadonly<T> = {
    readonly [key in keyof T]:T[key]
}

3.Tuple to Object

const tuple = ['a','b','c']

type TupleToObject<T extends readonly PropertyKey[]> = {
    [P in T[number]] : P
} 

type resTuple = TupleToObject<typeof tuple> // 为{'a':'a','b':'b','c':'c'}


4.First Of Array

type First<T extends any[]> = T extends [] ? never : T[0]

5.Length of Tuple

type LengthOfTuple<T extends readonly any[]> = T['length']

6.Exclude

// 运用extends的第二个用法,即 Distributive Conditional Types.
// T extends U 会分发性的把T的每个项去判断是否等于'a'
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
// 按官网的例子而言,我们假如需要一个string和number组合而成的数组类型,即(string | number)[]
// 写成type ToArray<Type> = Type extends any ? Type[] : never 导致 string[] | number[],由于Distributive Conditional Types的特性
//因此需要拿[]包裹,即type ToArray<Type> = [Type] extends [any] ? Type[] : never
type MyExclude<T,U> = T extends U ? never : T
type Result = MyExclude<'a' | 'b' | 'c', 'a'> // 'b' | 'c'

6.PickPartial

// 开发者在泛型里输入两个参数,interface和某个值并把它置为可选
interface User {
    id: number;
    age: number;
    name: string;
};

type PickPartial<T,K extends keyof T> = Partial<Pick<T, K>> & Omit<T,K>
type Result = PickPartial<User, 'id'>
// {
//     id?: number;
//     age: number;
//     name: string;
// }

7.Awaited

// type ExampleType = Promise<string>

// type Result = MyAwaited<ExampleType> // string

type MyAwaited<T extends PromiseLike<any | PromiseLike<any>>> = T extends PromiseLike<infer P> ?
    P extends PromiseLike<any> ? MyAwaited<P> : P : never 

8.If

type MyIf<F extends boolean,T ,K> = F extends true ? T : F extends boolean ? K : never 

9.Concat

// For example:
// type Result = Concat<[1], [2]> 
// expected to be [1, 2]
type MyConcat<T extends readonly unknown[],K extends readonly unknown[]> = [...T,...K]

10.Includes

type MyIncludes<T extends any[],K> = { 
    [key in T]:T[key]
}[K] extends true ? true : false

11.Push

// type Result = Push<[1, 2], '3'> // [1, 2, '3']
type MyPush<T extends readonly unknown[],U> = [...T,U]

12.Unshift

// type Result = Push<[1, 2], '3'> // [1, 2, '3']
type MyPush<T extends readonly unknown[],U> = [U,...T]

13.Parameters

// const foo = (arg1: string, arg2: number): void => {}

// type FunctionParamsType = MyParameters<typeof foo> // [arg1: string, arg2: number]

type MyParameters<T extends (...args:any[]) => unknown> = T extends (...args:infer S) => unknown ? S : never
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值