首页 > 其他 > 详细

[Typescript] Exclude Properties from a Type in TypeScript

时间:2020-02-17 00:40:26      阅读:55      评论:0      收藏:0      [点我收藏+]

There might be cases where you would want to create a type while excluding some of the properties from a type. Let‘s say you have a database query on a users table where you are not selecting the password field. In this case, you will not be able to assign the query results to your User type because it is not going to have all the fields available.

In this lesson, we are going to learn how we can use TypeScript‘s Omit utility type to exclude properties from a type.

type Item = {
  name: string;
  description: string;
  price: number;
  currency: string;
};

type PricelessItem = Omit<Item, "price" | "currency">;

const item: PricelessItem = {
  name: "Laptop Bag",
  description: "Leather bag for laptop"
};

console.log(item);

 

[Typescript] Exclude Properties from a Type in TypeScript

原文:https://www.cnblogs.com/Answer1215/p/12319287.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!