-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53.typeofOperator.ts
35 lines (28 loc) · 1.02 KB
/
53.typeofOperator.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
// The `typeof` operator in TypeScript allows you to capture the type of a variable.
// Define a location with x and y coordinates.
const origin = {
xCoord: 5,
yCoord: 10
};
// Use the `typeof` operator to create a type based on the structure of `origin`.
type Coordinate2D = typeof origin;
// Create a new variable with the type `Coordinate2D`.
// This ensures that the variable matches the structure of `origin`.
const position: Coordinate2D = {
xCoord: 3,
yCoord: 4
};
// Log the `position` variable to the console.
console.log(position);
// Define a user profile with name and age properties.
const userProfile = {
fullName: 'Alice',
userAge: 25
};
// Use the `typeof` operator to create a type based on the structure of `userProfile`.
type UserProfileResponse = typeof userProfile;
// Define a function that processes a user profile response.
// The function expects an argument that matches the structure of `userProfile`.
function handleUserProfile(data: UserProfileResponse) {
console.log(data);
}