TypeScript 4.6 improves constructors


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

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

The first improvement in TypeScript 4.6 is the ability to have code in constructors before super(). In JavaScript classes, it is 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 code at the start of a constructor if its containing class had property initializers. TypeScript 4.6 is now much more lenient in this check and allows other code to run before super(). while ensuring that super() occurs at the top level before any reference to ‘this’.

Another enhancement is the addition of control flow analysis for unstructured discriminated unions. TypeScript can use discriminator properties to refine type analysis. If an individual property is destructured into a const declaration, TypeScript checks whether the destructured type is a discriminated union. If so, TypeScript can now restrict variable types based on checks of other variables

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

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

TypeScript 4.6 is available now.


More information

TypeScript on NuGet

TypeScript on GitHub

Related Articles

TypeScript 4.5 adds expected

TypeScript 4.4 improves control flow parsing

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 scope reports

TypeScript adds conditional types

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




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


About Author

Comments are closed.