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 thatsuper()
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.