📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Learn typescript at TypeScript Tutorial with Examples.
Table of Contents
- Simple Interface Example
- Interface Optional Properties Example
- Interface Readonly properties Example
- Extending Interfaces Example
- Implementing an Interface Example
- Interface for Array Type Example
1. Simple Interface Example
export interface Employee{
firstName: string;
lastName: string;
fullName(): string;
}
let employee: Employee = {
firstName : "ramesh",
lastName: "fadatare",
fullName(): string{
return this.firstName + " " + this.lastName;
}
}
console.log(employee.firstName);
console.log(employee.lastName);
console.log(employee.fullName());
C:\typescript-tutorial> tsc interfaces.ts
C:\typescript-tutorial> node interfaces.js
ramesh
fadatare
ramesh fadatare
"use strict";
exports.__esModule = true;
var employee = {
firstName: "ramesh",
lastName: "fadatare",
fullName: function () {
return this.firstName + " " + this.lastName;
}
};
console.log(employee.firstName);
console.log(employee.lastName);
console.log(employee.fullName());
2. Interface Optional Properties Example
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): {color: string; area: number} {
let newSquare = {color: "white", area: 100};
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
let mySquare = createSquare({color: "black"});
console.log(mySquare);
{ color: 'black', area: 100 }
3. Interface Readonly properties Example
interface Point {
readonly x: number;
readonly y: number;
}
let p1: Point = { x: 10, y: 20 };
p1.x = 5; // error!
4. Extending Interfaces Example
interface IAddress {
address: string;
}
interface IPerson extends IAddress{
firstName: string;
lastName: string;
}
interface IEmployee extends IPerson {
employeeCode: number;
}
let employeeObj: IEmployee = {
firstName: "ramesh",
lastName: "fadatare",
address: "pune",
employeeCode: 100
}
console.log(employeeObj);
{ color: 'black', area: 100 }
{ firstName: 'ramesh',
lastName: 'fadatare',
address: 'pune',
employeeCode: 100 }
5. Implementing an Interface Example
interface Employee {
name: string;
paymentPerHour: number;
workingHours: number;
calculateSalary(): number;
}
class Contractor implements Employee {
name: string;
paymentPerHour: number;
workingHours: number;
constructor(name: string, paymentPerHour: number, workingHours: number) {
this.name = name;
this.paymentPerHour = paymentPerHour;
this.workingHours = workingHours;
}
calculateSalary(): number {
return this.paymentPerHour * this.workingHours;
}
}
class FullTimeEmployee implements Employee {
name: string;
paymentPerHour: number;
workingHours: number;
constructor(name: string, paymentPerHour: number) {
this.name = name;
this.paymentPerHour = paymentPerHour;
}
calculateSalary(): number {
return this.paymentPerHour * 8;
}
}
let contractor: Employee;
let fullTimeEmployee: Employee;
contractor = new Contractor('Ramesh contractor', 10, 5);
fullTimeEmployee = new FullTimeEmployee('Ramesh full time employee', 8);
console.log(contractor.calculateSalary());
console.log(fullTimeEmployee.calculateSalary());
C:\typescript-tutorial> tsc interfaces.ts
C:\typescript-tutorial> node interfaces.js
50
64
6. Interface for Array Type Example
interface NumList {
[index:number]:number
}
let numArr: NumList = [1, 2, 3];
numArr[0];
numArr[1];
console.log(numArr);
// Array which return string
interface ProLangArray {
[index:number]:string
}
// use of the interface
let progLangArray : ProLangArray = ['C', 'C++', 'Java', 'Python'];
console.log(progLangArray);
[ 1, 2, 3 ]
[ 'C', 'C++', 'Java', 'Python' ]
Learn typescript at TypeScript Tutorial with Examples.
Comments
Post a Comment
Leave Comment