-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27.TypeNarrowing.ts
72 lines (61 loc) · 2.18 KB
/
27.TypeNarrowing.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Type narrowing in TypeScript allows you to narrow down the type of a variable within certain areas of your code.
// This function demonstrates type narrowing using the 'typeof' type guard.
function addPadding(inputText: string, paddingValue: string | number) {
// If paddingValue is a number, add that many spaces before the inputText.
if (typeof paddingValue === 'number') {
return Array(paddingValue + 1).join('-') + inputText;
}
// If paddingValue is a string, add the string before the inputText.
if (typeof paddingValue === 'string') {
return paddingValue + inputText;
}
// If paddingValue is neither a string nor a number, throw an error.
throw new Error("Expected string or number, got '" + paddingValue + "'.");
}
console.log(addPadding("TypeScript Rocks", 3)); // returns "---TypeScript Rocks"
console.log(addPadding("TypeScript Rocks", "*")); // returns "*TypeScript Rocks"
// This section demonstrates type narrowing using the 'instanceof' type guard.
class Feline {
purr() {
console.log('Purr!');
}
}
class Canine {
bark() {
console.log('Bark!');
}
}
type Pet = Feline | Canine;
function emitSound(pet: Pet) {
if (pet instanceof Feline) {
pet.purr(); // OK, because Feline has the purr method.
}
else if (pet instanceof Canine) {
pet.bark(); // OK, because Canine has the bark method.
}
else {
// The 'never' type represents values that should never occur.
// let _unreachableCode: never = pet;
}
}
// This section demonstrates type narrowing using the 'in' type guard.
type Circle = {
radius: number;
}
type Triangle = {
base: number;
height: number;
}
type GeometricShape = Circle | Triangle;
function calculateArea(shape: GeometricShape) {
// If the shape has a 'radius' property, it's a Circle.
if ('radius' in shape) {
return 3.14 * shape.radius * shape.radius;
}
// Otherwise, it's a Triangle.
else {
return 0.5 * shape.base * shape.height;
}
}
console.log(calculateArea({ radius: 5 })); // 78.5 (approx value for πr^2)
console.log(calculateArea({ base: 5, height: 10 })); // 25 (value for 0.5 * base * height)