Modern TypeScript tour

TS 4 recap

TypeScript 4 hardened everyday patterns: stricter unknown handling, variadic tuples, template literal types, and control flow that remembers more. Most migration guides for that era read as small cleanups, not rewrites.

The practical takeaway was simple: model data with precise literals and unions, then let narrowing do the rest. Later versions kept that direction and added sharper tools. The program builds one array from a first value and a rest list and prints it.

function pair<First, Rest extends Array<unknown>>(first: First, ...rest: Rest): Array<unknown> { return [first, ...rest]; } const both: Array<unknown> = pair("a", 1, true); console.log(both);

[ 'a', 1, true ]

satisfies and const

The satisfies operator checks an expression against a type without widening or changing its inferred shape. Autocomplete stays exact while mismatches still error.

Pair it with as const for literal collections that must never widen. Write the value once, and let the checker guard both the shape and the spelling. The program prints the first size and the palette accent.

const sizes = ["s", "m", "l"] as const; const palette = { accent: "teal" } satisfies Record<string, string>; console.log(sizes[0]); console.log(palette.accent);

s
teal

const enums vs unions

String unions now cover most cases that once needed enums. They are lightweight, tree shake cleanly, and produce clear hover types.

Declare the union once and annotate every picker with it. Misspellings fail at compile time with the full list of allowed values in the message. The program picks one size and prints it.

type Size = "s" | "m" | "l"; const pick: Size = "m"; console.log(pick);

m

Standard decorators

Standard decorators attach behavior to classes and methods through a short annotation above the declaration. A factory such as tag below is the body a decorator runs: applied above a method, it receives the class and the name of the method.

Async decorators pair naturally with the promises background material when setup or teardown awaits. Keep decorator logic tiny and put real work in plain functions. Called directly with a target and a name, tag prints both.

function tag(target: object, key: string): void { console.log(key); console.log(target); } tag({}, "save");

save
{}

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What did TypeScript 4 add that narrowed the everyday patterns?

    Answer

    Variadic tuples, template literal types and sharper control flow

    The release hardened the type system, and later versions kept the same direction.

  2. What does satisfies keep that an annotation would widen?

    Answer

    The exact inferred shape of the value

    The expression is checked against the type without the annotation replacing what the compiler inferred.

  3. What does as const do to an array literal?

    Answer

    It makes the values and the array readonly literals

    The literal types stay narrow, so a size can no longer widen to string.

  4. Why do string unions often replace enums today?

    Answer

    They are lightweight, tree shake cleanly and hover as clear types

    A union of strings covers the picker cases without an enum object at run time.

  5. What does this program print? function pair<First, Rest extends Array<unknown>>(first: First, ...rest: Rest): Array<unknown> { return [first, ...rest]; } console.log(pair("a", 1, true));

    Answer

    [ 'a', 1, true ]

    The first argument and the rest list are spread into one array, which Node prints in brackets.

  6. What does this program print? const sizes = ["s", "m", "l"] as const; console.log(sizes[0], sizes.length);

    Answer

    s 3

    The literal array keeps its three values, and the first one is printed with the length.

  7. What does this program print? type Size = "s" | "m" | "l"; const pick: Size = "m"; console.log(pick.toUpperCase());

    Answer

    M

    The value is a string at run time, so the string method works and the letter goes to upper case.

  8. What does this program print? const palette = { accent: "teal" } satisfies Record<string, string>; console.log(palette.accent);

    Answer

    teal

    The check passes, and the exact field is still there to read.

  9. What does the compiler say about this program? const sizes = ["s", "m", "l"] as const; const pick: "m" | "l" = sizes[0]; console.log(pick);

    Answer

    error TS2322: Type '"s"' is not assignable to type '"m" | "l"'.

    The first element has the literal type s, which is outside the two-value union.

  10. What does the compiler say about this program? type Size = "s" | "m" | "l"; const pick: Size = "xl"; console.log(pick);

    Answer

    error TS2322: Type '"xl"' is not assignable to type 'Size'.

    The union lists three values, and the misspelling is refused with them in the message.

Article author: Arthur Isaev