What's New in TypeScript 4.8

Microsoft released TypeScript 4.8 with a host of new features ranging from Find All References improvements to the ability to exclude specific files from auto-imports.

TypeScript, which as its name suggests is a strongly typed language along with being a strict syntactical superset of JavaScript, builds on the non-typed JavaScript by adding the ability to use optional static typing. It has been on a roll lately, recently cracking the top 10 in IEEE's language popularity ranking, while also seeing an explosion in job postings and vaulting past Java to crack the Stack Overflow top five languages.

TypeScript in a Nutshell
[Click on image for larger view.] TypeScript in a Nutshell (source: Microsoft).

Here's a summary of what's new in v4.8, along with links for more information.

  • Improved Intersection Reduction, Union Compatibility, and Narrowing: Here there are numerous seemingly small changes that nevertheless "represent fixes for many many paper cuts that have been reported over several years," Microsoft said, highlighting how --strictNullChecks received a series of correctness and consistency improvements that affect how intersection and union types work.
  • Improved Inference for infer Types in Template String Types: This affects a recently introduced new way to add extends constraints to infer type variables in conditional types.
  • --build, --watch, and --incremental Performance Improvements: Here the team introduced several optimizations designed to speed up code dealing with --watch and --incremental that, along with optimizations for project references builds using --build, reportedly cut down processing time of common operations by 10 percent to 25 percent in certain scenarios.
  • Errors When Comparing Object and Array Literals: Here, Microsoft's Daniel Rosenwasser in an Aug. 25 blog post compared the == operator used in many other languages (but not JavaScript) to check on value equality on objects, noting that in Python the == can be used to check if a list is empty by checking if a value is equal to an empty list, for example. He used this code as an example:
    if people_at_home == []:
        print("here's where I lie, broken inside. </3")

    "This is not the case in JavaScript, where == and === between objects (and therefore, arrays) check whether both references point to the same value. We believe that similar code in JavaScript is at best an early foot-gun for JavaScript developers, and at worst a bug in production code." Thus TypeScript no longer allows code like this:

    if (peopleAtHome === []) {
    //  ~~~~~~~~~~~~~~~~~~~
    // This condition will always return 'false' since JavaScript compares objects by reference, not value.
        console.log("here's where I lie, broken inside. </3")
  • Improved Inference from Binding Patterns: This affects how TypeScript will pick up a type from a binding pattern to make better inferences in certain cases, based on this pull request to "Limit type argument inference from binding patterns."
  • File-Watching Fixes (Especially Across git checkouts): This fixes a longtime bug where TypeScript had difficulty with some file changes in --watch mode and editor scenarios.
  • Find-All-References Performance Improvements: Developers running this command see improved functionality when aggregating references. "This reduced the amount of time TypeScript took to search a widely-used identifier in its own codebase by about 20 percent," Rosenwasser said.
  • Exclude Specific Files from Auto-Imports: Rosenwasser said this self-explanatory functionality can be useful in cases where developers can’t avoid having certain modules or libraries in a compilation but rarely want to import from them. "These modules might have lots of exports that can pollute the auto-imports list and make it harder to navigate, and this option can help in those situations."

Rosenwasser also detailed many correctness fixes and breaking changes, along with pointing to what's coming up in v4.9 in November, explained in detail in the TypeScript 4.9 Iteration Plan.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus