Lab 02: Template Literal Types

Time: 30 minutes | Level: Advanced | Docker: docker run -it --rm node:20-alpine sh

Template literal type interpolation, string intrinsic types, extracting parts with infer, typed event names, CSS property type safety.


Step 1: Setup

docker run -it --rm node:20-alpine sh
npm install -g typescript ts-node
mkdir /lab-adv02 && cd /lab-adv02
cat > tsconfig.json << 'EOF'
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true
  }
}
EOF

Step 2: Basic Template Literal Types


Step 3: String Intrinsic Utilities


Step 4: Extracting Parts with infer


Step 5: Typed Event Names Pattern


Step 6: CSS Property Type Safety


Step 7: SQL Query Builder Types


Step 8: Capstone — Full Template Literal Demo

Run:

📸 Verified Output:


Summary

Feature
Syntax
Result

Template literal

`${A}${B}`

String combination

Uppercase

Uppercase<'hello'>

'HELLO'

Lowercase

Lowercase<'HELLO'>

'hello'

Capitalize

Capitalize<'hello'>

'Hello'

Uncapitalize

Uncapitalize<'Hello'>

'hello'

infer in template

`prefix${infer Rest}`

Extract suffix

Cross product

`${A | B}${C | D}`

All combinations

Route params

Recursive infer

Type-safe routing

Last updated