-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtypes.ts
More file actions
44 lines (32 loc) · 866 Bytes
/
subtypes.ts
File metadata and controls
44 lines (32 loc) · 866 Bytes
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
// type ParentType = number | string;
// let parentVar: ParentType = "omg what a string!";
// let childVarNumber: number = 42;
// let childVarString: string = "another string goes here ;)";
// function echo(value: ParentType): void {
// console.log(value);
// }
// echo(parentVar);
// echo(childVarNumber);
// echo(childVarString);
class Person {
name: string;
age: number;
}
class Customer {
name: string;
age: number;
membership: string;
}
class Pilot extends Person {
license: "PPL";
}
function showName(human: Person): void {
console.log(human.name);
}
const person: Person = { name: "Alice", age: 22 };
const customer: Customer = { name: "Michael", age: 25, membership: "Gold" };
showName(person);
showName(customer);
// Customer is a child type of Person ;)
// (Can be assigned wherever person in expected)
// But not vice-versa :P