This is a cross-published article on type-level string manipulation. At Volley, we use Typescript to power our voice experiences. We highly value engineering excellence and safety, and do our best to create robust systems that the compiler can validate as correct.
This code does not represent what we do for our games, but does serve as a fun example of the strong capabilities of Typescript’s type system. The original article was published on code.lol.
String Deduplication on the Type Level
The string deduplication problem is a canonical one within computer science, serving a similar purpose as fizz-buzz in terms of being an example of a simple problem that a reasonably knowledgeable practitioner should be able to solve with minimal effort.
The problem appears in a few variants, but briefly one such variant is to remove duplicate letters in a given string, such that the string then has only one instance of any given letter.
I thought this problem would be a particularly interesting case study into Typescript 4.1’s powerful literal string types.
The first step of any string algorithm is to split the string into units that can be processed individually. In this case, we split a given string literal into an array of strings, each of length 1.
Split is defined essentially as a nested conditional type (using the ternary syntax), that recurses into itself to define the tuple type. The C type is inferred to be the first character of S, and the R type is inferred to be the rest. (corresponding to Character and Rest respectively).
Another important capability when working with strings is the ability to collapse an array of strings into one string, via concatenation. On the type level, we can implement this in a similar fashion as the above Split type.
With this pattern, we are essentially performing a reduce operation in a similar manner as you might do when implementing it in a combinator form, e.g. using the Y combinator instead of a reduce function as such.
For example, this is how the equivalent meaning would be written on the value level:
The next feature we need is the ability to enforce tuples to be unique, only allowing the first instance of a given element to exist, and filtering away all others. We do this by keeping track of letters we have already seen (R), and conditionally outputting to an output string tuple type (O).
This is somewhat complicated by a limitation of TS 4.1, namely the ability to construct an object with a given key, whereby the key type is a literal type. The way we work around that now is via Invert, which switches the keys and values of an object type.
Putting it Together
The string deduplication problem is essentially a composition between various simple operations. With the fundamental utilities we’ve defined, the actual deduplication type is concise: