Paul Krill
Editor at Large

TypeScript 4.1 arrives with template literal types

news
Nov 23, 20202 mins

The template literal type has the same syntax as template literal strings in JavaScript except it is used in type positions

typewriter typescript
Credit: Mira DeShazer

TypeScript 4.1, an upgrade to Microsoft’s popular open source language that adds types to JavaScript, is now available as a production release. The upgrade features template literal types and a multitude of other improvements.

The template literal type has the same syntax as template literal strings in JavaScript except it is used in type positions. When used with concrete literal types, a new string literal type is produced by concatenating the contents.

In addition to introducing template literal types, TypeScript 4.1 lets developers re-map keys in mapped types with a new as clause. This new clause lets developers leverage features like template literal types to create new property names based on old ones. Keys can be filtered by producing never, saving developers from having to use an extra Omit helper type in some cases.

TypeScript 4.0, the current major release, arrived in August. Other capabilities in TypeScript 4.1 include:

  • With a recursive conditional types capability, some restrictions are eased on conditional types, which are now able to immediately reference themselves within their branches, making it easier to write recursive type aliases. Microsoft cautions that recursive types should be used responsibly and sparingly.
  • A new flag, --noUncheckedIndexedAccess, provides a node where every property access or indexed access is considered potentially undefined. This flag can catch out-of-bounds errors but might be noisy for a lot of code. It is not automatically enabled by the --strict flag.
  • The paths option can be used without baseUrl, to avoid an issue in which poor paths have been used by auto-imports.
  • checkJs now implies allowJs, fixing a situation in which starting a checked JavaScript project required both allowJs and checkJs, which caused friction.
  • The React 17 framework’s jsx and jsxs factory functions are supported through two new options for the jsx compiler options react-jsx and react-jsxdev.
  • The JSDoc tag @see now has better support in editors for JavaScript and TypeScript.
  • In a breaking change, members marked as abstract can no longer be marked as async. In another breaking change, resolve parameters are no longer optional in Promise.

TypeScript 4.1 can be accessed through NuGet or 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