TypeScript 4.3 Is Out

TypeScript 4.3 brings many new features and enhancment to the language such as Separate Write Types on Properties, Template String Type Improvements, override and the --noImplicitOverride Flag, and more.

Recently, Microsoft announced the availability of TypeScript 4.3. The latest release brings many new features and enhancment to the language such as Separate Write Types on Properties, Template String Type Improvements, override and the --noImplicitOverride Flag, and more.

Well, TypeScript is a language that builds on JavaScript by adding syntax for static types. When you add types throughout your code, you’re making your intentions explicit, and TypeScript can type-check your code to catch errors like typos, logic errors, and more. The language also uses those types to power editor tooling, providing you nice features like accurate code-completion, rename, and go-to-definition. Features like the TypeScript compiler can help you just erase TypeScript syntax, leaving you with clean readable JavaScript that works anywhere.

You can get the latest version 4.3 through NuGet, or using npm with the command: npm install typescript.

TypeScript 4.3 allows enales users to specify types for reading and writing to properties. In the below example, the set accessor takes a broader set of types (strings, booleans, and numbers), but the get accessor always guarantees it will be a number. Now you can finally assign other types to these properties with no errors.

class Thing {
    #size = 0;

    get size(): number {
        return this.#size;
    }

    set size(value: string | number | boolean) {
        let num = Number(value);

        // Don't allow NaN and stuff.
        if (!Number.isFinite(num)) {
            this.#size = 0;
            return;
        }

        this.#size = num;
    }
}

Source: Microsoft

Also note that while considering how two properties with the same name relate to each other, TypeScript 4.3 will only use the "reading" type. "Writing" types are only considered when directly writing to a property. The release also adds syntax to interfaces/object types to support different reading/writing types on properties.

In earlier releases, while overriding methods in a subclass, users simply used the same name. This often leads to subtle errors when the base class changed but the subclasses weren’t updated. The latest release TypeScript 4.3 introduces the override keyword to explicitly mark methods as overridden. So in case, there is not a suitable base entry to override then an error is flagged. This also better communicates intent to the reader.

4.3 also brings a new --noImplicitOverride flag. When this feature is turned on, it becomes an error to override any method from a superclass unless you explicitly use an override keyword. 

In earlier releases the language introduced a new type construct: template string types - types that either construct new string-like types by concatenating or match patterns of other string-like types. 4.3 brings improvment that when a template string is contextually typed by a string-literal-like type, it will try to give that expression a template type. This also kicks in when inferring types, and the type parameter extends string. The other major change here is that TypeScript can now better-relate, and infer between, different template string types.

Other highlights of the release include :

  • ConstructorParameters Works on Abstract Classes
  • Contextual Narrowing for Generics
  • Always-Truthy Promise Checks
  • static Index Signatures
  • .tsbuildinfo Size Improvements
  • Lazier Calculations in --incremental and --watch Compilations
  • Go-to-Definition on Non-JavaScript File Paths

To learn more you can visit the official announcement here.



Next Recommended Reading Bot Framework SDK Version 4.3 Released