ts的工具类的作用和实现?

ts的工具类有以下七种:

1、partial 部分类型;

2、required必填类型;

3、readonly只读类型;

4、exclude排除类型;

5、extract提取类型;

6、pick/omit排除key类型;

7、returnType返回值类型;

如何实现:

//Partial
interface User {
   id:string;
   name:string;
}
const userA:Partial<User>={
    name:''123456''
}

//Required
interface User {
   id?:string;
   name:string;
}
const userA:Required<User>={
   id:'123123';
   name:'123';
}
//Readonly类型的数据只可以读,同上写法;
//omit
interface User {
   id:string;
   name:string;
   age:string;
}
const userA=Omit<User,'age'|'id'>
//pick
const userA=Pick<User,'age'>
//exclude
type A='1'|'2'|'3'|'4';
type B=Exclude<A,'4'>
//提取
type C=Extract<A,'3'|'4'>
//returnType
function f(){}
type A=ReturnType<typeof f>

 

posted @ 2025-02-17 01:36  鹿俊俊  阅读(39)  评论(0)    收藏  举报