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 new features of TypeScript 2.1?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you what are the new features of TypeScript 2.1.I hope you can get something after reading this article. Let's discuss it together.

TypeScript is a superset of JavaScript developed by Microsoft, providing the latest JavaScript features and optional static types. Recently, TypeScript 2.1was released. This version provides a more powerful type checker and allows developers to write more concise code. Here are the main new features that this release brings:

Downlevel async/await: although async/await has been provided in previous versions, downlevel async/await allows developers to use this feature in an ES3/ES5 environment.

Object Rest&Spread: this is a new proposal from ES2017 that allows developers to easily merge, disassemble, and partially copy objects. This feature has been widely used when using libraries like Redux. Object Spread allows developers to easily copy an object, as follows:

Let copy = {... original}

Similarly, we can merge several different objects, as shown below, and the merged object will have both foo, bar, and baz properties:

Let merged = {... foo,... bar,... baz}

Developers can also add new properties to the merged object. Object Rest is a relative process that excludes specific attributes, as follows:

Let {a, b, c,... defghijklmnopqrstuvwxyz} = alphabet

Keyof and Lookup types: in fact, the keyof operator is called an index type query (index type query), which is like a query for keys of an object type, as follows:

Interface Person {name: string; age: number; location: string;} let propName: keyof Person

The above code can also be written as follows:

Let propName: "name" | "age" | "location"

The Lookup type is called the index access type (indexed access types), and it looks like accessing an element, as follows:

Interface Person {name: string; age: number; location: string;} let a: Person ["age"]

"Mapping type (Mapped Types)": this is the most interesting feature of TypeScript 2.1. Suppose we have a Person type, as follows:

Interface Person {name: string; age: number; location: string;}

If we want to create a type with all Boolean attributes on this basis, we need to define an entirely new type:

Interface BooleanifiedPerson {name: boolean; age: boolean; location: boolean;}

With the mapping type, we can define BooleanifiedPerson as:

Type BooleanifiedPerson = {[P in "name" | "age" | "location"]: boolean}

With the keyof operator, the above code can be further simplified to:

Type BooleanifiedPerson = {[P in keyof Person]: boolean}

Partial type: allows developers to create all optional versions of existing types. In addition, TypeScript 2.1 also provides utility types such as Readonly, Record, and Pick, which you can click here to see how they are implemented.

In addition, TypeScript 2.1 relaxes the check for package imports so that TypeScript will no longer prompt errors because the relevant declaration file cannot be found as long as the user has installed it.

Interested readers can get TypeScript through NuGet or install it through npm:

Npm install-g typescript

Visual Studio 2015 users need to upgrade to Update 3 before installing through a special installation package. Visual Studio Code or Sublime Text users can also use the latest version of TypeScript according to the relevant instructions.

After reading this article, I believe you have a certain understanding of "what are the new features of TypeScript 2.1.If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!"

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

Internet Technology

Wechat

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

12
Report