• TypeScript 实用工具类型


    TypeScript 实用工具类型

    Partial
    Readonly
    Record
    Pick
    Omit
    Exclude

    Partial

    作用: 生成一个新类型 该类型和 T 拥有相同的属性 但是所有属性皆为可选项
    (构造类型)Type 将 Type 的所有属性设置为可选 该类型将返回表示输入类型的所有字类型

    Partial<Type>
    
    • 1
    Constructs a type with all properties of Type set to optional. 
    This utility will return a type that repressents all subsets of a given type.
    
    构造类型的所有属性都设置为可选的类型。
    此实用程序将返回一个表示给定类型的所有子集的类型。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    interface Person {
    	name: string;
    	age: number;
    }
    type anothor = Partial<Person>;
    // {name?: string, age?: number}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    function updateObject<T>(obj: T, props: Partial<T>) {
    	return { ...obj, ...props };
    }
    updateObject<Person>(person, {name: "李四"});
    
    • 1
    • 2
    • 3
    • 4

    Readonly

    作用: 生成一个新的类型 T 中的 K 属性是只读的 K 属性是不可修改的
    (构造类型)Type 把 Type 的所有属性设置为 readonly 意味着不能重新赋值

    Constructs a type with all properties of Type set to readonly, 
    meaning the properties of the constructed type cannot be reassigned.
    
    构造类型的所有属性都设置为只读的类型,这意味着无法重新分配构造类型的属性
    把类型中的属性都变成只读的  接收类型  返回类型
    
    • 1
    • 2
    • 3
    • 4
    • 5
    Readonly<Type>
    
    • 1
    interface Person {
    	name: string;
    	age: number;
    }
    // {readonly name: string, readonly age: number}
    
    const anthor: Readonly<Person> = {
    	name: "李四",
    	age: 40,
    }
    
    // 不可以修改
    // anthor.name = "张三"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Record

    作用: 定义一个对象的 key 和 value 类型
    构造一个类型 其属性名的类型为 Keys 属性值得类型为 Type
    这个工具可用来把某个类型的属性映射到另一个类型上

    Constructs an object type whose property keys are Keys and whose property values are Type. 
    This utility can be used to map the properties of a type to another type.
    
    • 1
    • 2
    Record<Keys, Type>
    
    • 1
    // 字典
    let employees = {
    	1: {id: 1, fullname: "John Doe", role: "Designer"},
    	2: {id: 2, fullname: "Ibrahima Fall", role: "Developer"},
    	3: {id: 3, fullname: "Sara Duckson", role: "Developer"}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    interface EmployeeType {
    	id: number,
    	fullname: string,
    	role: string,
    }
    
    let employees: Record<number, EmployeeType> = {
    	1: {id: 1, fullname: "John Doe", role: "Designer"},
    	2: {id: 2, fullname: "Ibrahima Fall", role: "Developer"},
    	3: {id: 3, fullname: "Sara Duckson", role: "Developer"}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Pick

    作用: 生成一个新的类型 该类型拥有 T 中的 K 属性集 新类型 相当于 T 和 K 的交集

    从类型 Type 中 挑选部分属性 keys 来构造类型

    Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type.
    
    通过从类型中选取一组属性键(字符串文字或字符串文字的并集)来构造类型。
    
    • 1
    • 2
    • 3
    Pick<Type, Keys>
    
    • 1
    interface Todo {
    	title: string;
    	description: string;
    	completed: boolean;
    }
    
    /*
    	type TodoPreview = {
    		title: string;
    		completed: boolean;
    	}
    */
    // Pick 单词意为  选择
    type TodoPreview = Pick<Todo, "title" | "completed">
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Omit

    作用: 生成一个新类型 该类型拥有 T 中除了 K 属性以外的所有属性

    接收类型 得到新类型 在新类型中不包含 keys
    从类型 Type 中获取所有属性 然后从中剔除 Keys 属性后构造一个类型

    Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals).
    
    • 1
    Omit<Type, Keys>
    
    • 1
    interface Todo {
    	title: string;
    	description: string;
    	completed: boolean;
    }
    /*
    	type TodoPreview = {
    		title: string;
    		completed: boolean;
    	}
    */
    type TodoPreview = Pick<Todo, "title" | "completed">;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Exclude

    作用: 如果 T 是 U 的子类型 返回 never 不是返回 T

    从类型 Type 中剔除所有可以赋值给 ExcludedUnion 的属性 然后构造一个类型

    Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.
    
    • 1
    Exclude<UnionType, ExcludedMembers>
    
    • 1
    // type T0 = "b" | "c"
    Type T0 = Exclude<"a" | "b" | "c" | "a" | "b">;
    
    • 1
    • 2
  • 相关阅读:
    按键中断控制实验
    OpenCV图像处理学习九,双边滤波器 (Bilateral Filter)和中位数滤波器 (Median Filter)
    设计模式——15. 模板方法模式
    洛谷 T284656 改试卷(paper)
    【Windows &MTU】Windows上最大传输单元MTU值的查看和设置
    第二证券:产业资本真金白银传递市场信心
    SSD算法
    JavaScript对象:我们真的需要模拟类吗?
    北京映急物流有限公司 面试.net软件工程师岗位
    西安邮电大学第三届网络安全技能大赛---PWN方向WP
  • 原文地址:https://blog.csdn.net/lhblmd/article/details/126704836