forked from wesbos/hot-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-or-another.ts
56 lines (44 loc) · 926 Bytes
/
one-or-another.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
interface CourseBase {
name: string;
// more properties here
}
interface FreeCourse extends CourseBase {
youtube: string;
}
interface PaidCourse extends CourseBase {
price: number;
}
type Course = FreeCourse | PaidCourse;
const myCourse: Course = {
name: 'TypeScript',
price: 69,
youtube: 'hehe',
};
// 🔥 Use TypeScript's `never` to enforce "one or the other" properties on a type
// You can use function overloading.
interface Candy {
name: string;
price: number;
}
interface Chocolate extends Candy {
nuts: boolean;
}
const candy: Candy = {
name: 'Snickers',
price: 1.99,
notReal: 'hehe',
};
const candy2 = {
name: 'Snickers',
price: 1.99,
notReal: 'hehe',
} as Candy;
const chocolate: Chocolate = {
name: 'Snickers',
price: 1.99,
nuts: true,
};
function logCandy(candy: Candy) {
console.log(candy.name);
}
logCandy({ name: 'Snickers', price: 1.99, notReal: 'hehe' });