TypeScript, actively maintained and updated by Microsoft, introduced many utility and quality-of-life improvements in version 5.x. This chapter highlights the most popular updates aimed at enhancing stricter and more flexible type safety.
Please note that these features are available only in TypeScript 5.x and later versions. |
Template Literal Types now enable the creation of more precise types using template literals. This allows for defining custom types based on the actual string values at compile time.
type Color = “red” | “green” | “blue”; type HexColor<T extends Color> = `#${string}`; // Usage: let myColor: HexColor<“blue”> = “#0000FF”; |
Index Signature Labels let us use computed property names to label index signatures, providing more descriptive type information for dynamic objects.
type DynamicObject = { [key: string as `dynamic_${string}`]: string }; // Usage: let obj: DynamicObject = { dynamic_key: “value” }; |