TypeScript 4.6 Improves Constructors
Written by Kay Ewbank   
Thursday, 03 March 2022

TypeScript 4.6 has been released with improvements including the ability to have code before super() in constructors, and control flow analysis for destructured discriminated unions.

TypeScript is a superset of JavaScript that adds optional static types which can be checked by the TypeScript compiler to catch common errors in your programs. TypeScript can use this information to help you avoid about mistakes like typos, missing arguments, or forgetting to check for null and undefined.

typescript_logo

 

The first improvement to TypeScript 4.6 is the ability to have code in constructors before super(). In JavaScript classes it’s mandatory to call super() before referring to 'this'. Until now, TypeScript's enforcement of this rule was rather too strict, resulting in an error if there was any code at the beginning of a constructor if its containing class had any property initializers. TypeScript 4.6 is now much more lenient in that check and permits other code to run before super(). while still ensuring that super() occurs at the top-level before any references to 'this'.

Another improvement is the addition of control flow analysis for destructured discriminated unions. TypeScript can use discriminant properties to narrow the type analysis. If an individual property is destructured into a const declaration, TypeScript checks to see if the destructured type is a discriminated union. If it is, TypeScript can now narrow the types of variables depending on checks of other variables

Other improvements include improved recursion depth checks and improvements to indexed access types meaning that TypeScript can now correctly infer to indexed access types which immediately index into a mapped object type.

There's also a new tool called the TypeScript Trace Analyzer. This gives a better view of the analysis showing when a type could be computationally expensive to create and compare against other types.

TypeScript 4.6 is available now.

typescript_logo

 

More Information

TypeScript On NuGet

TypeScript On GitHub

Related Articles

TypeScript 4.5 Adds Awaited

TypeScript 4.4 Improves Control Flow Analysis

TypeScript 4.3 Adds Separate Property Write Types

TypeScript 4.2 Release Candidate Available

New TypeScript Website Launched

TypeScript 4.1 Adds Temporal Literal Types

TypeScript 3.0 Adds Project References

TypeScript Adds Unused Span Reporting

TypeScript Adds Conditional Types

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Born This Day In 1926 John Kemeny, Co-Founder of BASIC
31/05/2022

John Kemeny, born in Hungary on May 31st 1926, was the co-author of BASIC, standing for Beginner's All-purpose Symbolic Instruction Code. Although it is thought of as the popular language of [ ... ]



ICRA 2022 It's A Wrap
05/06/2022

ICRA 2022, the flagship conference of the IEEE Robotics and Automation Society Conference took place in Philadelphia at the end of May. There were 8K participants from 97 countries with 4.7K of them a [ ... ]


More News

pythondata

 



 

Comments




or email your comment to: comments@i-programmer.info