You are here: Home / Topics / TypeScript - Basic Types of Data

TypeScript - Basic Types of Data

Filed under: TypeScript on 2024-03-18 11:01:22

JavaScript has three very commonly used primitives: string, number, and boolean. Each has a corresponding type in TypeScript. 

string represents string values like "Hello, world" 

number is for numbers like 42. 

JavaScript does not have a special runtime value for integers, so there’s no equivalent to int or float - everything is simply number 

boolean is for the two values true and false

Example 1:


index.ts:
class Employee {
private _employeeName: string;
private _employeeAge: number;
private _employeeStatus: boolean;
private _employeeSalary: number;

public get employeeName(): string {
return this._employeeName;
}
public set employeeName(value: string) {
this._employeeName = value;
}
public get employeeSalary(): number {
return this._employeeSalary;
}
public set employeeSalary(value: number) {
this._employeeSalary = value;
}
public get employeeStatus(): boolean {
return this._employeeStatus;
}
public set employeeStatus(value: boolean) {
this._employeeStatus = value;
}

public get employeeAge(): number {
return this._employeeAge;
}
public set employeeAge(value: number) {
this._employeeAge = value;
}
public toString = (): string => {
return `Employee: ${this.employeeName}, ${this.employeeAge}, ${this.employeeSalary}, ${this.employeeStatus}`;
}
}
let employee1 = new Employee();
employee1.employeeName = 'John';
employee1.employeeAge = 25;
employee1.employeeSalary = 56000;
employee1.employeeStatus = true;
console.log(employee1.toString());

Note: Execute the tsc index.ts --target es2015 command to compile typescript code, then execute the code using the node index.js command.

About Author:
J
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.