TypeScript Improves Never-Initialized Variables Checks
Written by Ian Elliot   
Monday, 21 October 2024

Microsoft has announced TypeScript 5.7 in beta, with improvements including stronger checks for variables that have never been initialized before use, and path rewriting for relative paths.

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.


The first improvement to TypeScript 5.7 is in the checks for variables that haven't been initialized. TypeScript has long-standing support for catching issues when a variable has not yet been initialized in all prior branches. However, until now there have been some places where this has failed. For example, if the variable is accessed in a separate function, the type system doesn’t know when the function will be called, so assumes that the variable will be initialized. The checks have been tightened in this version, so that although TypeScript 5.7 will still give some leeway on variables that could have been initialized, the type system is able to report errors when variables haven't ever been initialized.

The second improvement is a change to deal with tools that support the running of TypeScript code "in-place", meaning they do not require a build step to generate output JavaScript files. The blog post explaining the improvement mentions ts-node, tsx, Deno, Bun, and Node.js. A problem can arise if developers want to generate .js files out of .ts files created like this, because the import path is relative but the output path needs to be absolute, and until now TypeScript has avoided rewriting any paths. There's now a new compiler option that will rewrite the path to be absolute.

Other improvements include support for --target es2024, which allows users to target ECMAScript 2024 runtimes; faster Project ownership checks in editors for composite projects; and validated JSON imports when importing from a .json file under --module nodenext so TypeScript will now enforce certain rules to prevent runtime errors.

TypeScript 5.7 is available in beta now.

More Information

TypeScript On NuGet

TypeScript On GitHub

Related Articles

TypeScript 5.6 Tightens Truthy And Nullish Checks

TypeScript 5.5 Adds ECMAScript Set Support

Node.js Adds Experimental TypeScript Support

TypeScript 5 - Smaller, Simpler, Faster

TypeScript 4.7 Adds Node.js ECMAScript Module Support

TypeScript 4.6 Improves Constructors

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


JetBrains Makes WebStorm and Rider Free for Non-Commercial Use
24/10/2024

JetBrains has launched a non-commercial license for its JavaScript and TypeScript IDE, WebStorm, and for Rider, its cross-platform .NET and game development IDE.



CSS Ecosystem In the Spotlight
06/11/2024

The 2024 edition of the State of CSS has been posted, revealing that the latest features of the language not only do away with extra tooling, but even start taking on tasks that previously requir [ ... ]


More News

espbook

 

Comments




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

Last Updated ( Monday, 21 October 2024 )