-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypesAndInterference.ts
75 lines (60 loc) · 1.26 KB
/
TypesAndInterference.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
73
74
75
// TypeScript infers the type as number
let inferredNumber = 42;
// TypeScript infers the type as string
let inferredString = "Hello, TypeScript!";
// TypeScript infers the type as boolean
let inferredBoolean = true;
type User = {
name: string;
age: number;
};
interface Persons {
name: string;
age: number;
}
interface Vehicle {
make: string;
model: string;
year: number;
}
interface Car extends Vehicle {
isElectric: boolean;
}
const myCar: Car = {
make: "Tesla",
model: "Model S",
year: 2021,
isElectric: true,
};
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
}
class Manager implements Employee {
name: string;
employeeId: number;
constructor(name: string, employeeId: number) {
this.name = name;
this.employeeId = employeeId;
}
}
type VehicleT = {
make: string;
model: string;
year: number;
};
type CarT = VehicleT & {
isElectric: boolean;
};
const myCarT: CarT = {
make: "Tesla",
model: "Model S",
year: 2021,
isElectric: true,
};
//Use interfaces when you need to define the shape of an object,
// especially if you expect to extend or merge them.
// Use types for more complex type definitions like
// unions, intersections, and for aliasing primitive types.