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:
Learn TypeScript on Mimo
- 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
.tsfile - 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.
letage:number=30;
age=31;
You can also rely on type inference:
letcount=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.
constname:string="Alex";
Reassigning causes a compile-time error:
name="Jordan";// Error
Inference works here too:
constisActive=true;
Prefer const by default. Switch to let only when reassignment is required.
What to look for
- Declaration syntax:
let variable: Typeorconst variable: Type - Type inference works when you assign a value immediately
- Without a type and without an initial value,
let score;may becomeany - Enable
noImplicitAnyintsconfig.jsonto avoid accidentalany - 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[];orlet user: { id: number }; - Avoid
var, it is function-scoped and can cause unexpected behavior
Examples you can copy
Example 1: Counter logic
letcounter=0;
counter+=1;
The variable changes over time, so let is appropriate.
Example 2: Configuration value
constapiUrl:string="https://api.example.com";
The value should not change, so const is safer.
Example 3: Delayed assignment
CSS
letcurrentUser: { 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:
vartotal=100;
Why it breaks: var is function-scoped and ignores block scope, which can cause unexpected behavior.
Correct approach:
lettotal=100;
Use let or const for block-scoped variables.
Mistake 2: Declaring without type or value
You might write:
letuser;
Why it breaks: TypeScript may infer any, which removes type safety.
Correct approach:
letuser: { id:number; name:string }|null=null;
Add an explicit type or assign an initial value.
Troubleshooting
- If you see “Type implicitly has an
anytype,” add a type annotation or enablenoImplicitAny. - 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
letfor variables that change - Use
constby default for fixed values - Add
: Typefor explicit type annotations - Avoid untyped declarations without initial values
- Enable strict settings to prevent implicit
any - Do not use
varin modern TypeScript
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot