Ezelu Joseph
2 min readDec 30, 2023
implicit variable declaration in typescript

Typescript is a strong typed programming language, enhanced developer experience and has wide acceptance in the industry.

In Typescript, you there are 2 ways you can declare a variable;
1. Implicit Declaration
2. Explicit Declaration

In this article, I’ll explain about Implicit variable declaration, the next article would be about Explicit variable declaration.

Implicit variable declaration is a kind of variable declaration where Typescript automatically detects and infers the data type of the variable based on the value assigned to it.

You don’t directly point out the data type of the variable but rather lets Typescript figure it out on it’s own based on the value you assigned to the variable (Typescript is good at this tho 😉)

Example of implicit variable declaration below:

// Implicit declaration based on assigned value
const name = 'Ezelu';
const age = 21;
const isFunny = true;

// TypeScript infers the types based on the assigned values
console.log(typeof name); // Output: string
console.log(typeof age); // Output: number
console.log(typeof isFunny); // Output: boolean

From the above example, Typescript automatically infers the type of variables based on the value passed to it.

This actually helps developers write codes faster without going through the extra process of inferring the data type manually.

It is worth noting that in Typescript, when a datatype is being assigned to a variable, you can’t modify the values of the variable to values of another datatype.
Below is an example of this

// These following operations wouldn't work and would throw errors
let name = 'Joe';
name = 12;

let age = 50;
age = false;
let fruits = ['orange', 'pear', 'banana'];
fruits[0] = 5;
let details = {
name: 'Tunde',
age: 23,
lovesFootball: true,
};
details.age = 'hey';
Ezelu Joseph
Ezelu Joseph

Written by Ezelu Joseph

Frontend Developer | React and Typescript Freak

No responses yet