Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the new features of TypeScript4.0

Shulou Source: shulou.com Published: 2022-06-01 12:27:31 09月18日 Update

This article mainly introduces "how to use the new features of TypeScript4.0". In daily operation, I believe many people have doubts about how to use the new functions of TypeScript4.0. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the new features of TypeScript4.0". Next, please follow the editor to study!

The main updates of version 4.0 are as follows:

Variable parameter tuple type

Marked tuple elements

Class attribute inference of constructor

Short circuit allocation operator

Unknown in catch clause

Customized JSX factory

Speed increase in build mode with-noEmitOnError parameters

-incremental with-noEmit

Editor improvement

Convert to optional link

Support / * * @ deprecated * /

Partial editing mode at startup

Smarter automatic import

Breaking Changes

Class attribute inference of constructor

When noImplicitAny is enabled, TypeScript 4.0 can now use control flow parsing (control flow analysis) to determine the type of attributes in the class.

Class Square {/ / Previously: implicit any! / / Now: inferred to `number`! Area; sideLength; constructor (sideLength: number) {this.sideLength = sideLength; this.area = sideLength * * 2;}}

If not all paths to the constructor are assigned to instance members, this property may be considered undefined.

Class Square {sideLength; constructor (sideLength: number) {if (Math.random ()) {this.sideLength = sideLength;}} get area () {return this.sideLength * * 2; / ~ / / error! Object is possibly 'undefined'. }}

In clearer cases (such as having some kind of initialize method), explicit type comments and fixed assignment assertions (!) may be required if you are in strictPropertyInitialization.

Class Square {/ / definite assignment assertion / / v sidelighting: number; / / ^ / / type annotation constructor (sideLength: number) {this.initialize (sideLength)} initialize (sideLength: number) {this.sideLength = sideLength;} get area () {return this.sideLength * * 2;}} short circuit allocation operator

JavaScript and many other languages support compound assignment operators. The compound assignment operator applies an operator to two parameters, and then assigns the result to the left. As follows:

/ Addition// a = a + ba + = bba ba / Subtraction// a = a-ba-= bumbago / Multiplication// a = a * ba * = baptism / Division// a = a / ba / = Exponentiation// a = a * * ba * * = bbot / Left Bit Shift// a = a

Tags: Operators operations new features properties learning functions parameters types assignments support three methods more patterns logic help inference utility clarity next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology vpn Shulou Tech Info Microsoft