Version 4.8 appears with the announced innovations for null checks and allows files to be excluded from automatic imports.
After a beta version and a release candidate, Microsoft has finally released the TypeScript 4.8 programming language. A new feature is the ability to adjust automatic imports to optionally exclude files. The features already announced with the release of the beta and the RC, such as more consistent zero checks and stricter handling of unconstrained generics, are also on board.
That’s how the guy can unknown
which corresponds to the Union Type {} | null | undefined
resembles, too null
, undefined
and accept any other types. Since the beta, TypeScript 4.8 has therefore allowed assignments of unknown
to {} | null | undefined
:
function f(x: unknown, y: {} | null | undefined) {
x = y; // always worked
y = x; // used to error, now works
}
File exclusion of your choice
With TypeScript 4.8, an editor preference is available to exclude certain files from auto-imports. In the source code editor Visual Studio Code, file names or globs can be inserted under “Auto Import File Exclude Patterns” in the settings UI. Alternatively, the file names can be stored in a .vscode/settings.json file:
{
// Note that `javascript.preferences.autoImportFileExcludePatterns` can be specified for JavaScript too.
"typescript.preferences.autoImportFileExcludePatterns": [
"**/node_modules/@types/node"
]
}
The new feature should be useful, for example, when certain modules or libraries cannot be avoided in a compilation, but you rarely want to import from there.
Increased performance and breaking changes
The new version tweaks the performance at various points: scenarios all around --watch
, --incremental
and --build
should be faster. With a fairly large internal code base, this can save 10 to 25 percent of time on many simple, common operations, according to the TypeScript team. The results can be viewed on GitHub.
One of the backwards incompatible innovations is that types cannot be imported or exported to JavaScript files. Previously, TypeScript allowed the import and export of entities written in import
– and export
– Statements were given a type but no value. However, this caused an error because named imports and exports for values that do not exist cause an error under ECMAScript modules.
In TypeScript 4.8, checking a JavaScript file using --checkJs
or by // @ts-check
-Comment out an error.
// @ts-check
// Will fail at runtime because 'SomeType' is not a value.
import { someValue, SomeType } from "some-module";
/**
* @type {SomeType}
*/
export const myValue = someValue;
/**
* @typedef {string | number} MyType
*/
// Will fail at runtime because 'MyType' is not a value.
export { MyType as MyExportedType };
The TypeScript team is already working on the next minor version TypeScript 4.9, as interested parties can follow using the iteration plan. Accordingly, the first beta should appear on September 20, 2022. The programming language, which is a superset of JavaScript, ranks ninth among the languages most in demand on the job market in the current analysis by the professional association IEEE.