How to Declare Variables in TypeScript

What you’ll build or solve

You’ll declare variables that behave predictably and work well with TypeScript’s type system.

When this approach works best

Declaring variables correctly works best when you:

  • Write new TypeScript code from scratch
  • Refactor JavaScript into TypeScript
  • Want clearer intent around reassignment and scope

It also helps when working in teams, where consistent variable patterns reduce confusion.

This is a bad idea only if you ignore types and rely on implicit behavior. That removes most of TypeScript’s benefits.

Prerequisites

  • TypeScript installed
  • A .ts file
  • Basic knowledge of JavaScript variables

Step-by-step instructions

Step 1: Declare with let for reassignable values

Use let when the variable’s value will change.

let age: number = 30;

age = 31;

You can also rely on type inference:

let count = 10;

TypeScript infers count as number.

Use let for counters, accumulators, or values that update over time.


Step 2: Declare with const for fixed values

Use const when the variable should not be reassigned.

const name: string = "Alex";

Reassigning causes a compile-time error:

name = "Jordan"; // Error

Inference works here too:

const isActive = true;

Prefer const by default. Switch to let only when reassignment is required.


What to look for

  • Declaration syntax: let variable: Type or const variable: Type
  • Type inference works when you assign a value immediately
  • Without a type and without an initial value, let score; may become any
  • Enable noImplicitAny in tsconfig.json to avoid accidental any
  • You can use union types in declarations: let id: string | number;
  • You can declare without initializing: let total: number; total = 50;
  • You can declare arrays and objects: let scores: number[]; or let user: { id: number };
  • Avoid var, it is function-scoped and can cause unexpected behavior

Examples you can copy

Example 1: Counter logic

let counter = 0;

counter += 1;

The variable changes over time, so let is appropriate.


Example 2: Configuration value

const apiUrl: string = "https://api.example.com";

The value should not change, so const is safer.


Example 3: Delayed assignment

let currentUser: { id: number; name: string } | null = null;

currentUser = { id: 1, name: "Sam" };

The variable is declared first and assigned later.


Common mistakes and how to fix them

Mistake 1: Using var instead of let or const

You might write:

var total = 100;

Why it breaks: var is function-scoped and ignores block scope, which can cause unexpected behavior.

Correct approach:

let total = 100;

Use let or const for block-scoped variables.


Mistake 2: Declaring without type or value

You might write:

let user;

Why it breaks: TypeScript may infer any, which removes type safety.

Correct approach:

let user: { id: number; name: string } | null = null;

Add an explicit type or assign an initial value.


Troubleshooting

  • If you see “Type implicitly has an any type,” add a type annotation or enable noImplicitAny.
  • If reassignment fails, confirm the variable was not declared with const.
  • If a variable behaves unexpectedly in a block, check that you are not using var.
  • If TypeScript infers a narrower type than expected, add an explicit annotation.

Quick recap

  • Use let for variables that change
  • Use const by default for fixed values
  • Add : Type for explicit type annotations
  • Avoid untyped declarations without initial values
  • Enable strict settings to prevent implicit any
  • Do not use var in modern TypeScript