Programming language: TypeScript 4.1 allows templates in string literal types
Source: Heise.de added 23rd Nov 2020After a two-month beta phase, TypeScript 4.1 has now been released. The current version allows templates in string literal types and introduces recursive conditional types. In addition, developers can now set the key for mapped types manually using a new keyword.
Well combined types with templates 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}`; // corresponds to: type Greeting = “hello world”; The new feature is particularly interesting when linking several string literal types, which make the product of the respective options as allowed elements results, as in the following code from the blog with reference to the well-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 because in Quantity the corresponding literal does not exist.
Free choice of key 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 example = { ?: boolean}; or as an extension of other types as in type Partial = {[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.
Self-related types 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 the type-checking of recursive types.
The blog post therefore advises to use it sparingly and leads along With a wink, two extreme examples: “It may be fun trying to model the values for the Collatz problem or the Fibonacci sequence, but just don’t publish it as a .d.ts file on npm!”
Strict index Also worth mentioning is the newly introduced TypeScript parameter – noUncheckedIndexedAccess for access to Index Signatures:
interface Options {path: string; permissions: number; // Extra properties are caught by this index signature. [propName: string]: string | number; } So far, TypeScript assumed with the above construct 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 such properties as in foo.bar or via the index as in foo [“bar”] potentially undefined .
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 anyway, and the combination checkJs: true, allowJs: false is prohibited, as emphasized by the pull request for 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 found on 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 with npm install -D typescript to install.
( rme)
brands: RME media: Heise.de
Related posts
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88
Related Products
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91