Write a program to calculate salary in TypeScript
Last Updated :
23 Jul, 2025
In this article, we will understand how we could create a basic Salary Calculator in TypeScript by using simple mathematical logics and operators (like divide("/") or multiply ("*") or addition ("+")).
Syntax: Following is the syntax of enums:
enum enums_name {
.......
}
Syntax: Following is the class declaration
class class_name {
constructor () {
.....
}
// Methods and variables.....
}
Following are the examples which show enums and class output on execution over the console.
Example 1: In this example, we will simply create enum and class and their randomly passed results.
JavaScript
enum fruits_name {
APPLE = 'Apple',
MANGO = 'Mango',
GRAPES = 'Grapes'
}
console.log('Enums Output : ');
console.log(fruits_name);
class Person {
name : string;
constructor (name : string) {
this.name = name;
}
displayName () : void {
console.log(`Name of a Person is : ${ this.name}`);
}
}
const person = new Person('GeeksforGeeks');
person.displayName();
Output:
Enums Output :
{ APPLE: 'Apple', MANGO: 'Mango', GRAPES: 'Grapes' }
Name of a Person is : GeeksforGeeks
Now that we have understood in detail about enums and classes let us dive into our task of creating a basic Salary Calculator through the following example.
Example 2: In this example, we will create a basic salary calculator, in which the user will pass a total salary amount and further our logic will separate Basic Salary, Medical amount, House Rent amount, and Conveyance amount and displays the result over the console.
JavaScript
enum SalaryDivision {
// Following values could be considered in %
Basic = 50,
HouseRent = 30,
Medical = 10,
Conveyance = 10
}
class SalaryCalculator {
GrossSalary : number;
constructor(GrossSalary : number) {
this.GrossSalary = GrossSalary;
}
displaySalary() : void {
let BasicSalary : number = (this.GrossSalary
* SalaryDivision.Basic) / 100;
let HouseRent : number = (this.GrossSalary
* SalaryDivision.HouseRent) / 100;
let Medical : number = (this.GrossSalary
* SalaryDivision.Medical) / 100;
let Conveyance : number = (this.GrossSalary
* SalaryDivision.Conveyance) / 100;
console.log("Basic Salary : " + BasicSalary);
console.log("HouseRent : " + HouseRent);
console.log("Medical : " + Medical);
console.log("Conveyance : " + Conveyance);
}
}
let salary1 = new SalaryCalculator(1000000);
let salary2 = new SalaryCalculator(50000000);
salary1.displaySalary();
salary2.displaySalary();
Output:
Basic Salary : 500000
HouseRent : 300000
Medical : 100000
Conveyance : 100000
Basic Salary : 25000000
HouseRent : 15000000
Medical : 5000000
Conveyance : 5000000
Reference: https://www.typescriptlang.org/docs/handbook/enums.html
Similar Reads
How to write a function in Typescript ? Writing a function in TypeScript is similar to writing it in JavaScript but with added parameters and return type. Note that any JavaScript function is a perfectly valid TypeScript function. However, we can do better by adding type.Syntax: Let's see a basic TypeScript function syntax (with two argum
4 min read
How to Parse Float with Two Decimal Places in TypeScript ? In Typescript, parsing float with two decimal places refers to mainly converting the float number with two digits after the decimal points. There are various approaches to parse float with two decimal places in TypeScript which are as follows: Table of Content Using Math.round methodUsing toFixed me
2 min read
Calculator App Using TypeScript A calculator app is a perfect project for practising TypeScript along with HTML and CSS. This app will have basic functionalities like addition, subtraction, multiplication, and division. It provides a clean and interactive interface for the user while using TypeScript to handle logic safely and eff
6 min read
Top 15 TypeScript Projects With Source Code TypeScript is a powerful, statically typed superset of JavaScript that enhances code quality and maintainability, making it a popular choice for modern web development. From building scalable applications to creating robust front-end and back-end systems, TypeScript brings structure and reliability
6 min read
Getting Started with TypeScript TypeScript is an open-source programming language developed by Microsoft that extends JavaScript by adding optional static typing to the language. It aims to make JavaScript development more scalable and maintainable, especially for large-scale projects. TypeScript code is transpiled into JavaScript
4 min read
How to Perform String Interpolation in TypeScript? String interpolation in TypeScript is defined as an expression that is used to evaluate string literals containing expressions. It replaces placeholders with evaluated results, allowing dynamic string generation from static text and variables, making it ideal for templating logic and enhancing code
2 min read