SEE: Linux commands for user management (TechRepublic Premium)
TypeScript 4.1 RC introduces a handful of new features that improve the programming language’s functionality and speed, and give developers greater control when writing code.
This includes the new template literal type. This acts as a building block for compiling other string types and allows developers to model functions and APIs that expect a set of specific strings.
“This is pretty nice because string literal types can basically spell-check our string values,” explained TypeScript program manager Daniel Rosenwasser.
“We also like that string literals can be used as property names in mapped types. In this sense, they’re also usable as building blocks.”
TypeScript 4.1 also allows developers to re-map keys in mapped types by using ‘as’ as a clause.
Mapped types were introduced in TypeScript 2.1, allowing developers to create new types from existing ones by mapping over property types.
SEE: Top 5 programming languages for systems admins to learn (free PDF) (TechRepublic)
Until now, mapped types could only produce new object types with keys already provided to them. “Lots of the time you want to be able to create new keys, or filter out keys, based on the inputs,” explained Rosenwasser.
“That’s why TypeScript 4.1 allows you to re-map keys in mapped types with a new as clause. This makes it easier to create property names based on old ones, by using template literal types.”
The latest version of TypeScript also eases some restrictions on conditional types: in TypeScript 4.1, conditional types can now immediately reference themselves within their branches, making it easier to write recursive type aliases.
Rosenwasser warned that these should be used sparingly as they can increase type-checking time, and also hit recursion depth limits that can result in compile-time errors.
The full list of new features in TypeScript 4.1 RC can be found here.