Skip to the content

Lesson 100 of 296

Basic types and type inference

What you will learn

  • Annotating basic types
  • Type inference: letting TypeScript work out types
  • Literal types and widening
  • Where you should write types and where you should not
On this page

Basic type annotations

let name: string = "Sam";
let age: number = 30;
let isAdmin: boolean = false;
let nothing: null = null;
let notSet: undefined = undefined;
let big: bigint = 100n;
let id: symbol = Symbol("id");

Type names are lowercase: string, not String. (String with a capital is the wrapper object type and should not be used for annotations.)

Type inference

TypeScript can usually work out the type by itself from the value. This is called type inference.

let city = "Leeds";        inferred: string
let count = 0;             inferred: number
city = 42;                 Error: Type 'number' is not assignable to type 'string'.

You did not write : string, but TypeScript still knows and protects the variable.

Literal types and widening

let a = "hello";       type: string
const b = "hello";     type: "hello"
  • A let can be reassigned, so TypeScript widens the type to string.
  • A const can never change, so its type is the exact literal type "hello".

A literal type is a type that allows only one exact value. They become very useful in unions (Lesson 103):

let direction: "left" | "right" = "left";
direction = "up";        Error

Where to write types

Let TypeScript infer when the type is obvious from the value:

const total = 0;
const names = ["Sam", "Ali"];         string[]

Write types in these places:

WhereWhy
Function parametersTypeScript cannot guess them (without a type they become any)
Return types of exported or important functionsDocuments the contract and catches mistakes inside the function
Variables declared without a valuelet user: User | null = null;
Empty arrays and objectsconst ids: number[] = []; (otherwise the type is unclear)
Values from outside (APIs, JSON)They are any or unknown by default
function formatPrice(pence: number): string {
  return `£${(pence / 100).toFixed(2)}`;
}

Hovering

In VS Code, hover over any variable to see its type. Use this all the time to check what TypeScript thinks.

Common mistakes

  • Annotating everything, which is noisy: const name: string = "Sam"; adds nothing.
  • Not annotating function parameters.
  • Using String, Number, Boolean (capitalised) as types.
  • Expecting a let variable to keep a literal type.

How to say it in an interview

"I annotate primitives with lowercase types and rely on inference where the type is obvious from the value. let widens literals to their general type, while const keeps the exact literal type. I always type function parameters, usually return types of exported functions, empty collections, variables initialised later, and anything coming from outside the program."

Key words

WordMeaning
Type inferenceTypeScript working out a type from the value
Literal typeA type that allows one exact value, like "left"
WideningA literal type becoming its general type, such as string
AnnotationA type written explicitly

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.