typescript常用工具类型

/**
 * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
 * 递归去解包 await 的值
 */
type Awaited<T> =
    T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
        T extends object & { then(onfulfilled: infer F, ...args: infer _): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
            F extends ((value: infer V, ...args: infer _) => any) ? // if the argument to `then` is callable, extracts the first argument
                Awaited<V> : // recursively unwrap the value
                never : // the argument to `then` was not callable
        T; // non-object or non-thenable

interface ArrayLike<T> {
    readonly length: number;
    readonly [n: number]: T;
}

/**
 * Make all properties in T optional
 * 所有T类型的属性,转为可选
 */
type Partial<T> = {
    [P in keyof T]?: T[P];
};

/**
 * Make all properties in T required
 * 所有T类型的属性,转为必选
 */
type Required<T> = {
    [P in keyof T]-?: T[P];
};

/**
 * Make all properties in T readonly
 * 所有T类型的属性,转为readonly
 */
type Readonly<T> = {
    readonly [P in keyof T]: T[P];
};

/**
 * From T, pick a set of properties whose keys are in the union K
 * 在一个类型中,获取某些字段,生成一个新类型
 * interface User {  
    uid : number;         // 用户ID  
    username : string;    // 用户名  
    password : string;    // 密码  
    email : string;       // 邮箱  
    residence : string;   // 居住地  
    job : string;         // 职业  
    sex : number;         // 性别  
    birthday : string;    // 生日
    }
    type RegisterArgs = Pick<User, 'username' | 'email' | 'password'>

    得到:
    interface RegisterArgs {  
        username : User['username'];  
        email : User['email'];  
        password : User['password'];
    }
    参考自:https://zhuanlan.zhihu.com/p/522017893
 */
type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

/**
 * Construct a type with a set of properties K of type T
 * 参考自:https://blog.csdn.net/weixin_38080573/article/details/92838045
 */
type Record<K extends keyof any, T> = {
    [P in K]: T;
};

/**
 * Exclude from T those types that are assignable to U
 * 从T中剔除可以赋值给U的类型。
 * type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "b" | "d"
 */
type Exclude<T, U> = T extends U ? never : T;

/**
 * Extract from T those types that are assignable to U
 * 提取T中可以赋值给U的类型。
 * type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "a" | "c"
 */
type Extract<T, U> = T extends U ? T : never;

/**
 * Construct a type with the properties of T except for those in type K.
 * 生成一个类型,排除掉给定类型中的某个字段
 * // 除了uid是必须的,其他都可以省略 
    interface UpdateUserArgs {  
        uid : User['uid']  
        username? : User['username'];  
        email? : User['email'];  
        residence? : User['residence'];  
        job? : User['job'];  
        sex? : User['sex'];  
        birthday? : User['birthday']
    }
    type UpdateUserArgs = Pick<User, 'uid'> & Partial<Omit<User, 'uid'>>
    选择User中的uid,确保其必填,然后让除了uid以外的所有字段可选填
    参考自:https://zhuanlan.zhihu.com/p/522017893
 */
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

/**
 * Exclude null and undefined from T
 * 给定类型排除掉null undefined
 */
type NonNullable<T> = T & {};

/**
 * Obtain the parameters of a function type in a tuple
 * 获取函数的参数类型
 * function test(a:string,b:number) {
        return {
            a,b
        }
    }
    type testtype = Parameters<typeof test>
    // type testtype = [a: string, b: number]

    // 获取的是 类型值
    type testtype1 = Parameters<typeof test>[1]
    // type testtype1 = number
    参考地址:https://blog.csdn.net/m0_52518047/article/details/127833927
 */
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

/**
 * Obtain the parameters of a constructor function type in a tuple
 */
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;

/**
 * Obtain the return type of a function type
 * 获取函数的返回值类型关键词:ReturnType
 * interface typeb {
        name: string,
        age: number,
    }
    let person:typeb = {
        name:'xiaoming',
        age:12
    }

    function test(person:typeb) {
        return person
    }
    type testtype2 = ReturnType<typeof test>
    // type testtype2 = typeb
    参考地址:https://blog.csdn.net/m0_52518047/article/details/127833927
 */
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

/**
 * Obtain the return type of a constructor function type
 */
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;

/**
 * Convert string literal type to uppercase
 */
type Uppercase<S extends string> = intrinsic;

/**
 * Convert string literal type to lowercase
 */
type Lowercase<S extends string> = intrinsic;

/**
 * Convert first character of string literal type to uppercase
 */
type Capitalize<S extends string> = intrinsic;

/**
 * Convert first character of string literal type to lowercase
 */
type Uncapitalize<S extends string> = intrinsic;

/**
 * Marker for contextual 'this' type
 */
interface ThisType<T> { }

/**
 * Represents a raw buffer of binary data, which is used to store data for the
 * different typed arrays. ArrayBuffers cannot be read from or written to directly,
 * but can be passed to a typed array or DataView Object to interpret the raw
 * buffer as needed.
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值