Skip to content

Commit b2e9b13

Browse files
committed
Interfaces In Depth
1 parent 8b1cbc9 commit b2e9b13

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

9. Interfaces/1. Interface.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Interface definition
2+
interface Computer {
3+
name: string;
4+
ram: number;
5+
hdd: number;
6+
}
7+
8+
// Usage
9+
const computerExample: Computer = {
10+
name: "i7",
11+
ram: 16,
12+
hdd: 23000,
13+
};
14+
15+
console.log(computerExample.name);
16+
console.log(computerExample.ram);
17+
console.log(computerExample.hdd);
18+
// ---------------------------
19+
20+
// ----------------------------------
21+
// Interface #1 (Simple Interface)
22+
interface Movie {
23+
readonly name: string; // 👈 Readonly Property
24+
ratings: number;
25+
genra?: string; // 👈 Optional Property
26+
}
27+
28+
const movie1: Movie = {
29+
name: "John Wick",
30+
ratings: 7,
31+
genra: "Action",
32+
};
33+
34+
// movie1.name = "Another Movie" // 👈 ERROR
35+
console.log(movie1);
36+
// ----------------------------------
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ---------------------------
2+
// Example 1
3+
4+
// Interface for a function
5+
interface MathOperation {
6+
(x: number, y: number): number;
7+
}
8+
9+
// Usage
10+
const add: MathOperation = (a, b) => a + b;
11+
const subtract: MathOperation = (a, b) => a - b;
12+
13+
console.log(add(5, 3));
14+
console.log(subtract(7, 2));
15+
// ---------------------------
16+
17+
// ---------------------------
18+
// Example 2
19+
20+
// Define an interface for a Car
21+
interface Car {
22+
brand: string;
23+
model: string;
24+
year: number;
25+
}
26+
27+
// Function that accepts an object adhering to the Car interface
28+
function displayCarInfo(car: Car): void {
29+
console.log(`Brand: ${car.brand}, Model: ${car.model}, Year: ${car.year}`);
30+
}
31+
32+
// Create an object that adheres to the Car interface
33+
const myCar: Car = {
34+
brand: "Toyota",
35+
model: "Camry",
36+
year: 2022,
37+
};
38+
39+
// Call the function with the object
40+
displayCarInfo(myCar);
41+
// ---------------------------
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Interface with method
2+
interface Person {
3+
firstName: string;
4+
lastName: string;
5+
age: number;
6+
sayHello(): void;
7+
}
8+
9+
function greet(person: Person) {
10+
console.log(`Hello, ${person.firstName} ${person.lastName}!`);
11+
person.sayHello();
12+
}
13+
14+
const john: Person = {
15+
firstName: "John",
16+
lastName: "Doe",
17+
age: 30,
18+
sayHello() {
19+
console.log("Hi there!");
20+
},
21+
};
22+
23+
const huxn: Person = {
24+
firstName: "HuXn",
25+
lastName: "WebDev",
26+
age: 18,
27+
sayHello() {
28+
console.log("What's good");
29+
},
30+
};
31+
32+
greet(john);
33+
greet(huxn);
34+
35+
// ----------------------------------
36+
// Interface #3 (Interface with method properties)
37+
interface Song {
38+
songName: string;
39+
singerName?: string;
40+
printSongInfo(songName: string, singerName: string): string;
41+
}
42+
43+
const song1: Song = {
44+
songName: "Natural",
45+
singerName: "Imagin Drigon",
46+
printSongInfo: (songName, singerName) => {
47+
return `Song: (${songName}) Singer: (${singerName})`;
48+
},
49+
};
50+
51+
console.log(song1.printSongInfo("Natural", "Imagin Drigon"));
52+
// ---------------------------
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Interface #5 (Extending Interface)
2+
interface MovieDetails {
3+
readonly name: string;
4+
ratings: number;
5+
printMovieInfo(name: string, price: number, ratings: number): string | number;
6+
}
7+
8+
interface MovieGenra extends MovieDetails {
9+
genra: string;
10+
}
11+
12+
const movie1: MovieGenra = {
13+
name: "John Wick",
14+
ratings: 7,
15+
printMovieInfo(name: string, price: number, ratings: number) {
16+
return `Name: ${name}, Price: ${price}, Ratings: ${ratings}`;
17+
},
18+
genra: "Action",
19+
};
20+
21+
const res = movie1.printMovieInfo("John Wick", 100, 8);
22+
console.log(res);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Interface for a class
2+
interface Vehicle {
3+
start(): void;
4+
stop(): void;
5+
}
6+
7+
// Class implementing the interface
8+
class Car implements Vehicle {
9+
start() {
10+
console.log("Car started");
11+
}
12+
13+
stop() {
14+
console.log("Car stopped");
15+
}
16+
}
17+
18+
// Usage
19+
const myCar = new Car();
20+
myCar.start();
21+
myCar.stop();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Original interface
2+
interface Car {
3+
brand: string;
4+
start(): void;
5+
}
6+
7+
// Declaration merging (interface extension)
8+
interface Car {
9+
model: string;
10+
stop(): void;
11+
}
12+
13+
// Usage of the extended interface
14+
const myCar: Car = {
15+
brand: "Toyota",
16+
model: "Camry",
17+
start() {
18+
console.log("Car started");
19+
},
20+
stop() {
21+
console.log("Car stopped");
22+
},
23+
};
24+
25+
// ------------------------------------
26+
interface User {
27+
firstName: string;
28+
lastName: string;
29+
age: number;
30+
}
31+
32+
interface User {
33+
printUserInfo(
34+
firstName: string,
35+
lastName: string,
36+
age: number
37+
): string | number;
38+
}
39+
40+
const huxn: User = {
41+
firstName: "HuXn",
42+
lastName: "WebDev",
43+
age: 18,
44+
printUserInfo(firstName, lastName, age) {
45+
return `Name: (${firstName} ${lastName}) Age: (${age})`;
46+
},
47+
};
48+
49+
console.log(huxn.printUserInfo("HuXn", "WebDev", 18));

9. Interfaces/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Interface
2+
3+
In TypeScript, an interface is a way to define a contract for a specific shape of an object. It defines the properties and methods that an object should have in order to be considered compatible with that interface.
4+
5+
To create an interface, you use the interface keyword followed by the name of the interface and the properties and methods it should contain, like this:
6+
7+
```ts
8+
interface Person {
9+
firstName: string;
10+
lastName: string;
11+
age: number;
12+
sayHello(): void;
13+
}
14+
```
15+
16+
In this example, we're defining an interface called Person that requires any object implementing it to have firstName, lastName, and age properties that are of type string, string, and number, respectively. It also requires an object implementing the Person interface to have a method called sayHello that takes no arguments and returns nothing (void).
17+
18+
You can then use this interface to check if an object matches its shape, like this:
19+
20+
```ts
21+
function greet(person: Person) {
22+
console.log(`Hello, ${person.firstName} ${person.lastName}!`);
23+
person.sayHello();
24+
}
25+
26+
const john: Person = {
27+
firstName: "John",
28+
lastName: "Doe",
29+
age: 30,
30+
sayHello() {
31+
console.log("Hi there!");
32+
},
33+
};
34+
35+
greet(john);
36+
```
37+
38+
In this example, we're defining a function called greet that takes a Person object as its argument and logs a greeting message using its firstName and lastName properties. We're also calling the sayHello method on the Person object.
39+
40+
Then, we're creating a john object that implements the Person interface and passing it to the greet function. Since john matches the shape of the Person interface, the code runs without any errors.
41+
42+
Interfaces are a powerful tool in TypeScript that allow you to enforce type safety and make your code more maintainable and readable.
43+
44+
# Extending Interfaces
45+
46+
You can use the extends keyword to extend an interface. Here's an example:
47+
48+
```ts
49+
interface Animal {
50+
name: string;
51+
}
52+
53+
interface Dog extends Animal {
54+
breed: string;
55+
}
56+
```
57+
58+
In this example, we have an Animal interface with a name property. We then create a new Dog interface that extends the Animal interface using the extends keyword. The Dog interface adds a breed property to the base Animal interface.
59+
60+
When you extend an interface, the new interface inherits all the properties and methods of the base interface, and you can also add new properties or methods to the new interface.

0 commit comments

Comments
 (0)