10

TypeScript 4.6 Beta Improves Type Inference and Error Checking

 2 years ago
source link: https://www.infoq.com/news/2022/02/TypeScript-beta-release/?utm_campaign=infoq_content&utm_term=global
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

TypeScript 4.6 Beta Improves Type Inference and Error Checking

Feb 16, 2022 2 min read

The TypeScript team released version 4.6 beta, which introduces improvements to control flow analysis, class constructor definitions, error checks in JavaScript files, and others.

TypeScript can now narrow down the signature for a function parameter whose type is a discriminated union. This is another step towards improving TypeScript’s type inference and making it better at inferring the object types for you.

Given the following function:

type Func = (args: ["a", number] | ["b", string]) => void;
const foo: Func = (param) => {
    const [kind, payload] = param
    if (kind === "a") {
        payload.toFixed();  // 'payload' narrowed to 'number'
    }
    if (kind === "b") {
        payload.toUpperCase();  // 'payload' narrowed to 'string'
    }
}

In the above example, the value of the "payload" field is narrowed down based on the value of the "kind" field. This simplifies the creation of decision trees within your functions.

TypeScript will now also flag more syntax and binding errors in JavaScript files. With the new error highlighting, we can spot and fix a lot of errors in JavaScript files during the compile time.

For example, two declarations of a variable in the same scope will now lead to an error:

const myVar = "Hello";
const myVar = "World;
// error: Cannot redeclare block-scoped variable 'myVar'.

You can disable this check within your JavaScript files by adding // @ts-nocheck at the top of your file.

In TypeScript 4.6 beta, it is no longer an error to add code at the beginning of a constructor, before calling super().

Daniel Rosenwasser, TypeScript program manager, explains the key reasoning behind the improvement:

In TypeScript, it was previously an error to contain any code at the beginning of a constructor if its containing class had any property initializers.
This made it cheap to check that super() got called before this was referenced, but this ends up rejecting a lot of valid code.

With the release of the new version, this check has become more forgiving, allowing other code at the top of the class constructor while ensuring that any use of `this` does not occur before invoking `super().`

Some of the other improvements in TypeScript 4.6 beta are indexed access inference improvements, better recursion depth checks, and a TypeScript trace analyzer.

With improved index access inference, access types that index into a mapped object type are now correctly inferred by TypeScript.

Thanks to better recursion depth checks, TypeScript is now better at identifying errors caused by infinitely expanding generic types. As a result, the check-time for several libraries on DefinitelyTyped has been reduced by ~50%.

Trace Analyzer is a new tool to get a more digestible view of information related to computationally expensive types in your projects.

There are several breaking changes to consider when moving to TypeScript 4.6 beta. First, in object rest expressions, unspreadable members from a generic object will be now be dropped. Also, with this release, grammatical and binding errors will always be reported for JavaScript files.

The TypeScript programming language is open-source software licensed under the Apache 2 license.

Contributions and comments are welcome on the TypeScript project on GitHub and should adhere to the TypeScript contribution guidelines and the Microsoft open-source code of conduct.

About the Author

Iskander Samatov

Iskander Samatov is currently a Tech Lead at HubSpot. He was previously involved with several startups in legal tech such as Joinder, building communication platforms and streamlining processes for professionals in the legal industry. Iskander has developed a number of mobile and web apps, used by thousands of people, like Planly. His most recent project is https://yourtrail.io.

Iskander frequently blogs at https://isamatov.com.

Show more

Recommend

  • 32
    • www.tuicool.com 5 years ago
    • Cache

    Type inference

    Type inference is a major feature of several programming languages, most notably languages from the ML family like Haskell. In this post I want to provide a brief overview of type inference, along with a simple Python imp...

  • 49
    • www.tuicool.com 5 years ago
    • Cache

    Type inference debate: a C++ phenomenon?

    I read two C++ subreddit

  • 37

    The Clojurescript compiler has been granted the capability to infer types sinceversion 1.10.516. The benefits for us, the cljs devs, are both at compile time and at run time: At compile time, our Clojuresc...

  • 54
    • www.tuicool.com 5 years ago
    • Cache

    Tripping up type inference

    One of the main selling points of Haskell is that despite (or because) of its strong static type system, it frees us from the burden of having to spell out tedious type signatures everywhere. Type inference is a bl...

  • 18
    • steshaw.org 4 years ago
    • Cache

    Hindley-Milner Type Inference

    Robin Milner's type system with parametric polymorphism was a significant advance over the systems of Russell and Church. Arguably it is the ability to use type-variables that makes higher order logic a practical vehicle...

  • 11
    • smallcultfollowing.com 3 years ago
    • Cache

    An experimental new type inference scheme for Rust

    An experimental new type inference scheme for Rust Jul 9, 2014 While on vacation, I’ve been working on an alternate type inference scheme for rustc. (Actually, I got it 99% working on the plane, and have been s...

  • 8

    Integrating binary data and type inference in SpiderMonkey Jul 19, 2013 Since the new version of PJS is going to be based on binary data, we are going to need to have a well-optimized binary data implementation...

  • 5
    • smallcultfollowing.com 3 years ago
    • Cache

    Type inference in Spidermonkey

    Type inference in Spidermonkey Jul 30, 2012 I’ve been slowly learning how type inference works in SpiderMonkey. As I understand it, SpiderMonkey’s type inference sche...

  • 4

    Julia 1.7 Extends its Threading Capabilities, Improves Type Inference, and More Dec 07, 2021...

  • 2

    TypeScript 4.7 and 4.8 Beta Releases Add ESM for Node.js and Better Type Inference Jul 29, 2022...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK