Skip to main content

Command Palette

Search for a command to run...

Explicit Variable Declaration in Typescript

Published
โ€ข1 min read
E

I am a Frontend React Developer ๐Ÿฅฐ๐Ÿฅฐ

A touch of goodness, Next.js and Typescript

I am passionate about Blockchain Development and Technologies ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ’›๐Ÿ’›

Explicit Variable Declaration is a type of variable declaration where the developer explicitly let's Typescript know the variable datatype without utilizing the automatic Typescript datatype inference.

This is particularly useful for both the compiler and but especially for other developers working on the same project. They can easily know what a variable is all about by glancing across the codebase.

let name: string; // Explicit Variable Declaration
name = 'Joseph'; // Assigning "Joseph" to name

let isRunning: boolean;
isRunning = false;

let age: number = 30;

let about: string | number; // Union Types
about = 'Funny';
about = 40;

Note:

Union Types is a way to assign various datatypes to a variable in Typescript. In the above example, we let Typescript know that the about variable can contain both string and number datatypes