In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what practical types are available in TypeScript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Practical type
TypeScript natively provides several useful practical types to help us with some common type conversions. These utility types are available globally, and they all use generics.
The following seven are the ones I often use.
1. Pick
Pick picks the property set Keys from Type to create a new type, and Keys can be a literal string or a literal union of strings. The value of Keys must be the key of Type, otherwise the TypeScript compiler will complain. This utility type is especially useful when you want to create lighter objects by selecting certain properties from objects with many properties.
Type User = {name: string age: number address: string occupation: string} type BasicUser = Pick// type BasicUser = {/ / name: string;// age: number;//}
2. Omit
Omit is the opposite of Pick. Keys does not mean which attributes to retain, but rather the key set of attributes to be omitted. This is more useful when we just want to remove some properties from the object and keep others.
Type User = {name: string age: number address: string occupation: string} type BasicUser = Omit// type BasicUser = {/ / name: string;// age: number;//}
3. Partial
Partial constructs a type whose type properties are set to optional. This can be useful when we are writing update logic for an object.
Type User = {name: string age: number address: string occupation: string} type PartialUser = Partial// type PartialUser = {/ / name?: string;// age?: number;// address?: string;// occupation?: string;//}
4. Required
Required is the opposite of Partial. It constructs a type that all attributes are required. It can be used to ensure that no optional attributes appear in a type.
Type PartialUser = {name: string age: number address?: string occupation?: string} type User = Required// type User = {/ / name: string;// age: number;// address: string;// occupation: string;//}
5. Readonly
Readonly builds a type whose properties are set to read-only. Reassigning a new value TS will report an error.
Type User = {name: string age: number address: string occupation: string} type ReadOnlyUser = Readonlyconst user: ReadOnlyUser = {name: "Xiao Zhi", age: 24, address: "Xiamen", occupation: "Great shift to the World"} user.name = "Wang Daye" / / Cannot assign to 'name' because it is a read-only property.
6. ReturnType
ReturnType builds a type from the return type of a function type. It is useful when we deal with function types from external libraries and want to build custom types based on them.
Import axios from 'axios'type Response = ReturnTypefunction callAPI (): Response {return axios ("url")} Thank you for reading! this is the end of this article on "what are the practical types in TypeScript?" I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.