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

How to construct the typescript utility type

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail "typescript utility type how to construct", the content is detailed, the steps are clear, the details are handled properly, I hope this article "typescript utility type how to construct" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Partial

Construct a type with all properties Type set to optional. This utility returns types that represent all subsets of a given type.

Interface Todo {

Title: string

Desc: string

}

Function updateTodo (todo: Todo, fieldsToUpdate: Partial) {

Return {... todo,... fieldsToUpdate}

}

Const todo1 = {

Title: 'organize desk'

Desc: 'clear clutter'

}

Const todo2 = updateTodo (todo1, {

Desc: 'throw out trash'

});

Required

Construct a type that consists of all the properties of Typeset to required

Interface Props {

A?: number

B?: string

}

Const obj: Props = {a: 5}

Const obj2: Required = {a: 5}

Readonly

Construct a type readonly with all properties set to Type, which means that properties of the construction type cannot be reassigned.

Interface Todo {

Title: string

}

Const todo: Readonly = {

Title: "Delete inactive users"

}

Todo.title = "Hello"

Record

Construct an object type with the property key Keys and the property value Type. This utility can be used to map attributes of one type to another.

Interface CatInfo {

Age: number

Breed: string

}

Type CatName = 'miffy' |' boris' | 'mordred'

Const cats: Record = {

Miffy: {age: 10, breed: 'Persian'}

Boris: {age: 5, breed: 'Maine Coon'}

Mordred: {age: 16, breed: 'British Shorthair'}

}

Pick

Construct a type Keys by selecting a set of attributes from the Type (string literals or union of string literals).

Interface Todo {

Title: string

Desc: string

Completed: boolean

}

Type TodoPreview = Pick

Const todo: TodoPreview = {

Title: 'clean room'

Desc: 'ddddd'

}

Omit

Construct a type by selecting all the attributes Type from it and then removing Keys (string literals or union of string literals).

Interface Todo {

Title: string

Description: string

Completed: boolean

CreatedAt: number

}

Type TodoPreview = Omit

Const todo: TodoPreview = {

Title: 'Clean room'

Completed: false

CreatedAt: 1615544252770

}

ReturnType

Construct a type Type that consists of the return type of function.

After reading this, the article "how to construct typescript utility types" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, please 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