Skip to main content

Command Palette

Search for a command to run...

TypeScript Unlocked: Types

Updated
3 min read
TypeScript Unlocked: Types
P

"Hello, I'm Palash Dhavle, a passionate software developer with a Master's Degree in Computer Applications. I specialize in crafting Python and JavaScript code to build dynamic applications. Through my blog, I share insights from my coding journey and explore the world of software development. Join me in this adventure of creativity, innovation, app development, and problem-solving. Welcome!

💡
This article is a part of TypeScript Unlocked Series on my blog. Every article in the series is short, crisp and filled with examples and code snippets. If you want to learn TypeScript from scratch for absolutely FREE!!! check it out.

What are types?

To understand Types we can take example of real world Caste System. In Caste system people are divided into different groups based on the work they do and they are treated differently based on their castes. I know its not fair but this is how it used to be in some parts of the world and this system is practiced even today in some rural parts of India and other 3rd world countries.

Just like the Caste system we have different types in Typescript and each type has its own property. Like Type of Number will only accept numbers type of String will only accept string and type of Boolean will only accepts Boolean and so on..

Syntax

const variableName : type = value

Example

const age : number = 24

so this is how we define types for any variable. in this example we have used the number type which is used to represent a number obviously.

let's look at some of the basic types which we have in TypeScript.

  1. Number:

    • Represents numeric values (integers or floating-point numbers).

    • Examples: let count: number = 5;, let pi: number = 3.14;

  2. String:

    • Represents textual data enclosed within single (') or double (") quotes.

    • Examples: let message: string = 'Hello, TypeScript!';, let name: string = "Alice";

  3. Boolean:

    • Represents logical values true or false.

    • Examples: let isValid: boolean = true;, let hasError: boolean = false;

These 3 are primitive types in Plain JavaScript and has a corresponding type in TypeScript which we can use to do type Notations. There are variety of inbuild types and we can also create our own types. we'll Learn about this as we progress in the series.

There is one more important type

  1. Any

    • Any means we are telling TypeScript to not do any type checking of the the variable

    • Examples: let car: any = "BMW";, let bike: any = 5;

Inferred types

TypeScript is smart enough to automatically assign the type to the variable looking at the value you have assigned to the variable.

let num : number = 5

you can also write this as

let num = 5

even though you have not mentioned the type, typescript will automatically assign one to it. In future if you try to assign a String or any other value to num TypeScript will warn you automatically.

In many cases its a good practice to rely on inferred types because its obvious of what type the variable should be by looking at the value but there are some exceptions where we should avoid using type inference.

So this post covers the crux of types we'll be looking at different types and how to use them in later as we go further into the series.

You can also checkout TypeScript Docs for more info on types.

if you reached till here and you liked what you read don't forget to share it with other people. if you have any feedback or suggestions feel free to use the comment section. Hope you tag along till the end of this series and strengthen your coding armor by adding TypeScript to it. Happy coding!

TypeScript Unlocked

Part 7 of 8

TypeScript is a JavaScript Wrapper which helps you to write better JavaScript code by introducing types. In this series we will learn how to Install and use TypeScript to write cleaner and better code

Up next

TypeScript Unlocked: Introduction

💡 This article is a part of TypeScript Unlocked Series on my blog. Every article in the series is short, crisp and filled with examples and code snippets. If you want to learn TypeScript from scratch for absolutely FREE!!! check it out. Hey there,...

More from this blog

T

Tron Codes

15 posts

Hello, I'm Palash Dhavle, a passionate software developer with a Master's Degree in Computer Applications. I specialize in crafting Python and JavaScript code to build dynamic applications.