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.
let count = 0; // number const limit = 10; // 10 (literal type) let label: string; // annotation, assigned later const names = ["a", "b"]; // string[]
Try it
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.
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.
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
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
constis error TS2588 ("Cannot assign to 'total' because it is a constant."). - A
constwithout 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
letwith no initializer and no annotation has the implicit typeanythat evolves with assignments; undernoImplicitAnythe 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 forletonly when the variable truly changes. - Widening rules.
let x = "a"becomesstring, andlet n = 1becomesnumber. Write an annotation when you want a union:let id: string | null = null. constis shallow. Objects and arrays assigned to aconstcan still be mutated. Addas constorreadonlyto 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
ReferenceErrorat run time, and TypeScript reports the common cases at compile time. - Loops. A
letin aforheader creates a fresh binding per iteration, so closures capture each value separately. Aconstis allowed infor...ofandfor...inheaders. - Narrowing sticks better with
const. Narrowing of aletvariable is reset when it is captured and modified in a callback, while aconstkeeps its narrowed type inside closures. - Redeclaration. Declaring the same name twice in one scope is an error (TS2451), unlike
var.
Related
- var, type, readonly, if and else
- Lessons: Primitive types, Type inference, Literal types