Basic types

String

A string holds text between quotes. The annotation after the colon promises the compiler that only text will ever live in that constant:

const name: string = "Ada"; console.log(name.toUpperCase());

ADA

Because name is known to be a string, the editor can suggest string methods such as toUpperCase. Storing a number in name later would stop the compile with a clear error.

Number

One number type covers integers and decimals alike. There is no separate int or float, which keeps arithmetic annotations short:

const age: number = 36; const next: number = age + 1; console.log(next);

37

Both constants promise numbers, so adding them is checked while string concatenation by accident is rejected. Math stays math and text stays text.

Boolean

A boolean holds exactly two values: true or false. Conditions read them directly, with no comparison tricks needed:

const ready: boolean = true; if (ready) { console.log("go"); }

go

The if branch runs because ready is true. Assigning a string such as yes to a boolean would fail the compile, which keeps flags honest across the codebase.

Let TS infer

Annotations can be omitted when the starting value already says it all. TypeScript infers string for the constant below, so adding the annotation would only repeat the obvious:

let city = "Paris"; console.log(city.length);

5

Inference is not looseness: city is still a string, and storing a number in it later still fails. Write explicit annotations at function borders and let inference handle the plain cases inside.

Questions

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

  1. Which annotation promises that a constant holds text?

    Answer

    const name: string = "Ada";

    The type follows the colon, and the primitive type of text in TypeScript is string.

  2. How many number types does TypeScript have for ordinary arithmetic?

    Answer

    One: integers and decimals are both number

    number covers integers and decimals alike, so arithmetic annotations stay short.

  3. What does this declaration do? let city = "Paris";

    Answer

    It infers the type string from the starting value

    The starting value already says the type, so the compiler infers string and keeps it.

  4. What does this program print? const name = "Ada"; console.log(name.length);

    Answer

    3

    The inferred type is string, and length counts the three letters.

  5. What does this program print? const age: number = 36; const next: number = age + 1; console.log(next);

    Answer

    37

    The second constant stores the sum, which is 37.

  6. What does this program print? const ready: boolean = false; if (ready) { console.log("go"); } else { console.log("wait"); }

    Answer

    wait

    ready is false, so the else branch runs and prints its line.

  7. What does the compiler say about this program? const name: string = 42; console.log(name);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    A number cannot be stored in a constant promised to hold text.

  8. What does the compiler say about this program? const ready: boolean = "yes"; console.log(ready);

    Answer

    error TS2322: Type 'string' is not assignable to type 'boolean'.

    The two values of boolean are true and false, and a string is neither.

  9. What does this program print? console.log(typeof 42);

    Answer

    number

    typeof reports number for integers and decimals alike.

  10. What happens when this program is compiled? let city = "Paris"; city = 5; console.log(city);

    Answer

    error TS2322: Type 'number' is not assignable to type 'string'.

    The inferred type is string, so a number cannot be assigned to the same variable later.

Article author: Arthur Isaev