Template Literal Types in TypeScript

// #types#typescript // 1 comment

Template literal types are essentially types for strings. By defining the pattern that a string must match, these types provide a way to validate and infer data. They were introduced about three years ago in TypeScript 4.1. Following the original GitHub PR, the following examples show the versatile functionality TypeScript has gained through template literal types.

String Format Validation

TypeScript's template literals facilitate validation of string formats. In this example, an IPv4Address type is defined that uses template literals to enforce a specific string pattern (an IPv4 address).

// IPv4 address format, e.g. type IPv4Address = `${number}.${number}.${number}.${number}`; //> Error: Type '"192.168.0"' is not assignable to type '`${number}.${number}.${number}.${number}`' const badIpAddress: IPv4Address = '19216801'; //> Ok const goodIpAddress: IPv4Address = '';

The IPv4Address type uses a template literal to define the specific pattern of an IPv4 address. If a string does not match this format (for example, badIpAddress), TypeScript issues an error.

Extracting Parts From a String

Template literals can be used to extract components of a string, which is a similar function to parsing strings at compile time. The ExtractIpAddress type is intended to extract the four segments of an IPv4 address.

type ExtractIPv4Address<TIpAddress extends string> = TIpAddress extends `${infer A}.${infer B}.${infer C}.${infer D}` ? [A, B, C, D] : never; //> IPv4AddressParts = ["192", "168", "0", "1"] type IPv4AddressParts = ExtractIPv4Address<''>;

By using the TypeScript infer keyword within the template literal, each segment of the IP address can be extracted individually. The output is an array of strings corresponding to the segments.

Splitting a String By a Delimiter

Finally, we can use recursive template literal types to mimic the functionality of the split function in JavaScript. The Split type recursively splits a string S into segments around a delimiter D.

// Split a string by a delimiter type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S]; //> IPv4AddressParts = ["192", "168", "0", "1"] type IPv4AddressParts = Split<'', '.'> //> IPv6AddressParts = ["2001", "0db8", "85a3", "0000", "0000", "8a2e", "0370", "7344"] type IPv6AddressParts = Split<'2001:0db8:85a3:0000:0000:8a2e:0370:7344', ':'>;

šŸ’” Original Example from TypeScript Playground

If S can be split into two segments T and U around the delimiter D, the type returns an array with T and the result of Split<U, D>. The recursion continues until S cannot be split further and an array with S as the only element is returned.

Discover More Template Magic

You can find even more great examples in this GitHub repo. It's a goldmine of clever use cases that show the power of template literals in TypeScript.