TypeScript Dynamic Discriminant with Template String<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

TypeScript Dynamic Discriminant with Template String

Posted on: January 31, 2022

An improvement with TypeScript 4.5 compared to 4.4 is the ability to discriminate on a dynamically constructed type with a pattern. Template string type has been around for a few versions, but it was impossible to narrow down until 4.5. Before getting into the specific, here is an example of two interfaces: Weapon and Magic. The discriminant, the common property, is called kind. However, the type is not unique for the interface; it is dynamically adjustable. Hence, we can have data coming from the backend, giving us several weapons of a different kind that share the same schema (interface).

1export interface Weapon {
2 kind: `Weapon_${string}`;
3 range: number;
4 damage: number;
5}
6
7export interface Magic {
8 kind: `Magic_${string}`;
9 costMana: number;
10 multiplier: number;
11}
12
13export function attack(attackerItem: Weapon | Magic) {
14 if (attackerItem.kind === "Weapon_") {
15 console.log("Range:" + attackerItem.range);
16 } else if (attackerItem.kind === "Magic_") {
17 console.log("Mana:" + attackerItem.costMana)
18 }
19}

The code above compiles prior to TypeScript version 4.5 until the line compares the kind in the attach function.

1if (attackerItem.kind === "Weapon_") {

With TypeScript 4.5, it is possible to write the static portion of the type, and TypeScript will narrow down to the type allowing to use of the properties that are unique for the specific interface. So, in that example, the first condition is for weapon, and the code compiles (and has auto-complete) for the range, which is not available for the second condition.

Small detail, you must specify the whole static part and not rely on else. The following code does not compile.

1export function attack(attackerItem: Weapon | Magic) {
2 if (attackerItem.kind === "Weapon_") {
3 console.log("Range:" + attackerItem.range);
4 } else {
5 console.log("Mana:" + attackerItem.costMana)
6 }
7}

Another detail is that you cannot specify only a portion of the static name of the type. For example, the following code does not work.

1export function attack(attackerItem: Weapon | Magic) {
2 if (attackerItem.kind === "Weapon") {
3 console.log("Range:" + attackerItem.range);
4 } else if (attackerItem.kind === "Magic") {
5 console.log("Mana:" + attackerItem.costMana)
6 }
7}

Even though, as a human, we can figure out that we do not need the underscore to identify the type, TypeScript requires to have the whole string. Since we could have a type named TypeA and TypeAA, it makes sense, and having a partial Type would break.