Programming language: TypeScript 4.1 allows templates in string literal types

TypeScript 4.1 has now been released after a two-month beta phase. The current version allows templates in string literal types and introduces recursive conditional types. In addition, developers have recently been able to manually set the key for mapped types using a new keyword.

The newly introduced template string literal types enable the use of templates within string literal types. The syntax is the same as for the template string literals known from JavaScript, but does not refer to strings but to types as in the following “Hello World” example from the TypeScript blog:

type World = "world";
type Greeting = `hello ${World}`;
// entspricht:  type Greeting = "hello world";

The innovation is particularly interesting when linking several string literal types, which result in the product of the respective options as allowed elements, as in the following code from the blog with reference to the known Children’s book by Dr. Seuss:

type Color = "red" | "blue";
type Quantity = "one" | "two";

type SeussFish = `${Quantity | Color} fish`;
// same as
//   type SeussFish = "one fish" | "two fish"
//                  | "red fish" | "blue fish";

The code combines the two string literal types Color and Quantity and thus offers any combination of the respectively defined literals for number and color, but only for this: "three fish" would not be allowed as in Quantity the corresponding literal does not exist.

TypeScript 4.1 also enables the manual definition of keys for mapped types. With the latter, object types can be created using keys as in type Beispiel = {[K in "a" | "b" | "c"]?: boolean}; or as an extension of other types like in type Partial<T> = {[K in keyof T]?: T[K]};.

Developers can use the newly introduced keyword as define individual keys. In combination with the template literal types, new property names can be defined on the basis of existing names.

Another new addition to the current release are the recursive conditional types. They offer a more flexible handling of conditional types by allowing them to reference themselves. It should be noted, however, that TypeScript needs more time for type checking of recursive types.

The blog post therefore advises economical use and, with a wink, lists two extreme examples: “It may be fun to try to model the values ​​for the Collatz problem or the Fibonacci sequence, but don’t publish it as a .d.ts file on npm! ”

The newly introduced TypeScript parameter is also worth mentioning --noUncheckedIndexedAccess for access to Index Signatures:

interface Options {
  path: string;
  permissions: number;

  // Extra properties are caught by this index signature.
  [propName: string]: string | number;

Previously, TypeScript assumed that all properties were either of the type string or number must be. With the new flag, the compiler takes into account that direct access to properties such as those in or via the index as in foo["bar"] potentially undefined could be.

Another new feature is that TypeScript when activating the checking of JavaScript code via the command line parameter --checkJs or the configuration options.checkJs = true automatically options.allowJs = true puts. The permitted compilation of JavaScript content is a prerequisite for checking and combining checkJs: true, allowJs: false is forbidden like the Pull Request highlights the innovation. Until now, developers still had to set the value manually.

Other innovations such as the use of paths without baseUrl and some breaking changes can be made see the TypeScript blog. Compared to the beta, as is usual with TypeScript, no new features have been added. Developers can obtain the release from NuGet or via npm npm install -D typescript to install.


To home page