Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods worth knowing in TypeScript

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the methods worth understanding in TypeScript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The type system in TypeScript is very powerful. It provides us with type safety. The type system is popular, but it can also make our code confusing and difficult to read if we don't plan and design types and interfaces.

Generics

To avoid code duplication and create reusable types is an important part of writing concise code. Generics are a feature of TypeScript that allows us to write reusable types. Look at the following example:

Type Add = (a: t, b: t) = > Tconst addNumbers: Add = (a, b) = > {return a + b} const addStrings: Add = (a, b) = > {return a + b}

Put the correct type in the generic type of Add, which can be used to describe the addition of two numbers or the concatenation of two strings. We don't need to write a type for each function, we only need to do it once with generics. This not only saves us energy, but also makes our code more concise and less error-prone.

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")}

In addition to the above, there are other practical types that can help us write cleaner code. Links to TypeScript documentation about utility types can be found here.

The above is all the contents of this article "what are the methods worth knowing in TypeScript?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report