forked from wesbos/hot-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnever.ts
34 lines (31 loc) · 851 Bytes
/
never.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
// Great for exhaustive switch statement
type Bird = { kind: 'bird', legs: number; wings: 2; };
type Dog = { kind: 'dog', legs: number; };
type Fish = { kind: 'fish', fins: number; };
type Animals = Bird | Dog | Fish;
function animalAppendages(animal: Animals): number {
switch (animal.kind) {
case 'bird':
return animal.wings + animal.legs;
case 'dog':
return animal.legs + 1; // tail
case 'fish':
return animal.fins;
default:
// this should never happen
let neverHappens: never = animal;
return neverHappens;
}
}
type CurrencyOptions = 'CAD' | 'USD' | 'EUR';
function getRate(rate: CurrencyOptions): number {
if (rate === 'CAD') {
return 1.3;
}
else if (rate === 'USD') {
return 1;
}
// return( rate as never);
// const neverEver: never = rate;
// return neverEver;
}