typescript
48 lines · 8 steps
Type-level string parsing in TypeScript
A toolkit of recursive conditional types that splits, trims, and reshapes string literals entirely at the type level.
Explained by
highlit
1type Split<S extends string, D extends string> =
2 S extends `${infer Head}${D}${infer Tail}`
3 ? [Head, ...Split<Tail, D>]
4 : [S];
5
6type Trim<S extends string> =
7 S extends ` ${infer R}` ? Trim<R> :
8 S extends `${infer L} ` ? Trim<L> :
9 S;
10
11type ExtractParams<Path extends string> =
12 Path extends `${string}:${infer Param}/${infer Rest}`
13 ? Param | ExtractParams<`/${Rest}`>
14 : Path extends `${string}:${infer Param}`
15 ? Param
16 : never;
17
18type RouteParams<Path extends string> = {
19 [K in ExtractParams<Path>]: string;
20};
21
22type Capitalize<S extends string> =
23 S extends `${infer First}${infer Rest}`
24 ? `${Uppercase<First>}${Rest}`
25 : S;
26
27type CamelCase<S extends string> =
28 S extends `${infer Head}_${infer Tail}`
29 ? `${Head}${Capitalize<CamelCase<Tail>>}`
30 : S;
31
32type CamelKeys<T> = {
33 [K in keyof T as K extends string ? CamelCase<K> : K]: T[K];
34};
35
36type HexColor = `#${string}`;
37type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
38type Endpoint = `${HttpMethod} /${string}`;
39
40export type {
41 Split,
42 Trim,
43 RouteParams,
44 CamelCase,
45 CamelKeys,
46 HexColor,
47 Endpoint,
48};
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Template literal patterns with `infer` let you destructure strings into pieces the type checker can manipulate.
- 2Recursion plus a base case is how type-level computation iterates over a string until nothing matches.
- 3Key remapping in mapped types can transform property names while preserving their value types.
Related explainers
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
typescript
import { Component, Input } from '@angular/core'; interface Order { id: string;
How Angular ICU plurals localize an order summary
i18n
pluralization
standalone-component
Intermediate
8 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, concatMap, finalize } from 'rxjs'; import { DataSource, QueryRunner } from 'typeorm';
Wrapping requests in a transaction with NestJS
interceptors
transactions
rxjs
Advanced
7 steps
typescript
type CsvColumn<T> = { header: string; value: (row: T) => string | number | boolean | null | undefined; };
Building a type-safe CSV writer in TypeScript
generics
serialization
escaping
Intermediate
7 steps
typescript
import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js'; export interface NormalizedPhone { e164: string;
Normalizing phone numbers to E.164 in TypeScript
validation
normalization
error-handling
Intermediate
7 steps
typescript
import { CanActivate, ExecutionContext, Injectable,
Role-based access with a NestJS guard
authorization
guards
decorators
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/type-level-string-parsing-in-typescript-explained-typescript-d4ed/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.