Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript let and const

Reference for let and const in TypeScript: block scope, type inference, literal types for const, widening, annotations, the temporal dead zone and as const.

let and const declare block-scoped variables. A let variable can be reassigned; a const binding cannot. TypeScript adds a type to each one, usually by inferring it from the initializer so you do not have to write it.

The two keywords infer differently. A let variable gets the widened type of its initializer (let n = 5 is number), because you may assign other numbers later. A const keeps the narrowest type it can (const n = 5 has the literal type 5), because it can never change.

TypeScript
let count = 0;            // number
const limit = 10;         // 10 (literal type)
let label: string;        // annotation, assigned later
const names = ["a", "b"]; // string[]

Try it

TypeScript let widens, const keeps the literal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Reassigning a const is an error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Const objects are still mutable inside

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
let name: Type = initializer;
let name = initializer;
let name: Type;               // definitely assigned before use
const name: Type = initializer;
const name = initializer;     // initializer is required
let a = 1, b = "two";         // several declarators in one statement
Part Description
name The identifier, or a destructuring pattern such as { a, b } or [x, y].
: Type Optional type annotation. Without it TypeScript infers the type from the initializer.
initializer Required for const. Optional for let when an annotation is present.

Errors and edge cases:

  • Assigning to a const is error TS2588 ("Cannot assign to 'total' because it is a constant.").
  • A const without an initializer is a syntax error (TS1155).
  • Reading a variable before its declaration line in the same scope is error TS2448 ("Block-scoped variable used before its declaration").
  • A let with no initializer and no annotation has the implicit type any that evolves with assignments; under noImplicitAny the compiler tracks it through control flow and only complains if it cannot determine the type at a use.

Usage notes

  • Prefer const. It documents intent and gives narrower types. Reach for let only when the variable truly changes.
  • Widening rules. let x = "a" becomes string, and let n = 1 becomes number. Write an annotation when you want a union: let id: string | null = null.
  • const is shallow. Objects and arrays assigned to a const can still be mutated. Add as const or readonly to make the type reflect immutability (see readonly).
  • Temporal dead zone. Both keywords exist from the start of their block but cannot be read before the declaration runs. Accessing them early throws a ReferenceError at run time, and TypeScript reports the common cases at compile time.
  • Loops. A let in a for header creates a fresh binding per iteration, so closures capture each value separately. A const is allowed in for...of and for...in headers.
  • Narrowing sticks better with const. Narrowing of a let variable is reset when it is captured and modified in a callback, while a const keeps its narrowed type inside closures.
  • Redeclaration. Declaring the same name twice in one scope is an error (TS2451), unlike var.