-
Notifications
You must be signed in to change notification settings - Fork 0
/
31.IntersectionTypes.ts
48 lines (39 loc) · 1.47 KB
/
31.IntersectionTypes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Intersection types allow you to combine multiple types into one.
// This enables you to add together existing types to get a single type that has all the features you need.
// Define a 2D coordinate with x and y values.
type Coordinate2D = {
a: number;
b: number;
};
// Extend the 2D coordinate to a 3D coordinate by adding a z value.
type Coordinate3D = Coordinate2D & {
c: number;
};
// Define a basic user profile with just a username.
type UserProfile = {
username: string;
};
// Define an email structure.
type UserEmail = {
emailAddress: string;
};
// Define a phone structure.
type UserPhone = {
phoneNumber: number;
};
// A function to display contact information.
// This function expects an object that satisfies all three types: UserProfile, UserEmail, and UserPhone.
function displayContactInfo(profile : UserProfile & UserEmail & UserPhone) {
console.log(`Hello, I'm ${profile.username}. Email me at ${profile.emailAddress} or call me at ${profile.phoneNumber}.`);
}
displayContactInfo({
username: 'Bob',
emailAddress: "[email protected]",
phoneNumber: 9876543210
});
// For convenience, you can create a new type that combines all the three types.
type UserContactInfo = UserProfile & UserEmail & UserPhone;
// Another function using the combined type.
function displayUserContact(profile : UserContactInfo) {
console.log(`Hello, I'm ${profile.username}. Email me at ${profile.emailAddress} or call me at ${profile.phoneNumber}.`);
}