Assertions

The as keyword

The as keyword tells the compiler to treat a value as a narrower type. It never converts data at runtime, it only changes the static view. Reach for it when you know more than the checker can prove.

const input: unknown = "hi"; const text: string = input as string; console.log(text);

hi

Non-null !

The exclamation mark removes null and undefined from the type in one character. Use it only when logic above guarantees a real value. A wrong mark crashes at runtime with no compiler warning to save you.

function head(items: string[]): string { const first: string | undefined = items[0]; return first!; } console.log(head(["a", "b"]));

a

const assertions

Appending as const freezes a literal into its narrowest form. Arrays become readonly tuples and strings keep exact text types. Exact literals power safer option unions and config objects.

const verbs = ["get", "post"] as const; type Verb = (typeof verbs)[number]; const chosen: Verb = "get"; console.log(chosen);

get

Assertions can lie

Assertions bypass checks, so a wrong one compiles cleanly and fails later. Prefer guards and generics that prove the type instead of claiming it. When an assertion survives review, add a comment that states the reason.

const value = "5" as unknown as number; console.log(value + 1);

51

The compiler accepts the double assertion, so the constant is typed as number. At run time the value is still the string, and the plus joins instead of adding.

Questions

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

  1. What does input as string change at run time?

    Answer

    Nothing: only the static view of the value changes

    The assertion is erased with the types, so the running program keeps the value it had.

  2. What does the non-null ! remove from a type?

    Answer

    null and undefined

    The mark drops the two empty types, and a wrong mark leaves a crash for run time.

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

    Answer

    It makes a readonly tuple of the exact literal types

    The literal keeps its narrowest form, which is how typeof verbs[number] becomes a union of exact texts.

  4. Why is a wrong assertion dangerous?

    Answer

    It compiles cleanly and the mistake appears at run time

    The compiler believes the claim, so no check stands between the wrong type and the running code.

  5. What does this program print? const input: unknown = "hi"; const text: string = input as string; console.log(text);

    Answer

    hi

    The assertion only narrows the static type, and the value is still the string.

  6. What does this program print? function head(items: string[]): string { const first: string | undefined = items[0]; return first!; } console.log(head(["a", "b"]));

    Answer

    a

    The array has an item at zero, so the mark hides a missing case that does not happen here.

  7. What does this program print? const verbs = ["get", "post"] as const; type Verb = (typeof verbs)[number]; const chosen: Verb = "get"; console.log(chosen);

    Answer

    get

    The chosen value is one of the two exact texts, and the program prints it.

  8. What does this program print? const value = "5" as unknown as number; console.log(value + 1);

    Answer

    51

    The value is still the string at run time, so + joins it with 1 instead of adding.

  9. What does the compiler say about this program? const n = 42 as "hello"; console.log(n);

    Answer

    error TS2352: Conversion of type 'number' to type '"hello"' may be a mistake because neither type sufficiently overlaps with the other.

    A number and a text literal overlap too little, so the compiler refuses the direct assertion and names the route through unknown.

  10. What does this program print? function head(items: string[]): string { return items[0]!; } console.log(head([]));

    Answer

    undefined

    The mark only silences the compiler, so the empty array returns undefined and the program prints it.

Article author: Arthur Isaev