Search⌘ K
AI Features

TypeScript Setup and Data Types

Explore setting up TypeScript and get a clear understanding of fundamental data types such as strings, numbers, booleans, arrays, and enums. This lesson helps you grasp how TypeScript enforces data type consistency, improving code quality and maintainability in Node.js applications.

Overview

TypeScript is a strictly typed language. It ensures that the data types of variables, return value of functions, class methods, members, and so on are consistent over the lifecycle of our application.

TypeScript data types

Below are the basic data types in TypeScript:

  • String
  • Number
  • Boolean
  • Tuple
  • Array
  • Any
  • Void
  • Unknown
  • Object
  • Null
  • Undefined
  • Never
  • Enum

For this lesson, we’ll explore just a few of these.

String, number, and boolean

Declaring a string, number, or boolean variable is as simple, as shown below:

TypeScript 3.3.4
var myName: string = "John Doe"
var myAge: number = 25
var isVerified: boolean = false
console.log(`My name is ${myName} and I am ${myAge} old but my verification status is ${isVerified}`)

Array

Below, we show how to declare a number, string, or boolean array:

TypeScript 3.3.4
let strPhoneList: string[] = ["(555) 888 87373", "+90 3839 3933"]
var numPhoneList: number[] = [20, 5, 3, 22]
var bolList: boolean[] = [false, true]
console.log()
console.log(numPhoneList)
console.log(bolList)

Enum

Enum is a data type that enables us to define standard constants in a way that is easy to remember and maintain. In TypeScript, we have number and string-based enums. We define enums using the enum keyword.

TypeScript 3.3.4
enum ESIZE {
L = 1,
XL = 2,
XXL = 3
}
enum EFOOD {
EGG = "EGG",
DRINK = "DRINK",
MEAT = "MEAT"
}
console.log(EFOOD.EGG)
console.log(ESIZE.XL)

TypeScript provides more data types that aren’t available in JavaScript, which makes developing with TypeScript more professional and maintainable.