Paul Krill
Editor at Large

TypeScript 3.8 adds type-only imports and exports

news
Feb 21, 20203 mins

Now available in a production release, TypeScript 3.8 provides more fine-grained control over imports and elisions

type writer and white laptop back to back on table top
Credit: Thinkstock

TypeScript 3.8, a new version of Microsoft’s typed superset of JavaScript, is now available as a production release. The latest TypeScript upgrade emphasizes type-only imports and exports and ECMAScript standards compliance.

The release adds a new syntax for type-only imports and exports, with the intent of giving users more fine-grained control over import and elision. The import type syntax only imports declarations to be used for type annotations and declarations and always gets fully erased. The export type syntax only provides an export to be used for type contexts and is erased from TypeScript output.

TypeScript has used JavaScript’s import syntax to enable reference types. While convenient, the capability only worked because of the import elision feature, which omits TypeScript type imports when TypeScript outputs JavaScript files. However, import elision has been both insufficient and problematic.

In conjunction with import type, TypeScript 3.8 also adds a compiler flag to control what happens with imports that won’t be used at runtime: importsNotUsedAsValues.

Regarding the ECMAScript standard for JavaScript, TypeScript 3.8 adds support for ECMAScript private fields, which is part of a class fields declaration proposal for JavaScript. Public and private fields will be integrated into single, orthogonal whole, according to this proposal.

Other improvements promised in TypeScript 3.8 include:

  • “Fast and loose” incremental type-checking, featuring a compiler option, assumeChangesOnlyAffectDirectDependencies. This option can reduce build times in certain files, as TypeScript will recheck/rebuild only files that have changed as well as files that directly import them. It is recommended for large code bases where developers are willing to defer full project errors until a later time.
  • Implementation of the export as * ns syntax, providing a single entry point to expose all members of another module as a single member.
  • Top-level await, an upcoming ECMAScript feature to remove restrictions on using an await expression at the top of a file under specific conditions.
  • JavaScript files are supported in TypeScript 3.8 by turning on the allowJs flag; type-checking of those files is supported via the checkjs option or by adding a // @ts-check comment to the top of .js files. New JSDoc tags are leveraged for properties, because JavaScript files lack a dedicated syntax for type-checking.
  • A watchOptions field in tsconfig.json and jsconfig.json lets users tell the compiler/language service which file watching strategies to use to keep track of files and directories.
  • A refactoring to convert string concatenations.
  • Stricter assignability checks to unions with index signatures, a breaking change.
  • In another breaking change, object in JSDoc is no longer any under noImplicitAny.

You can install the general release of TypeScript 3.8 through NuGet or via NPM:

npm install typescript

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author