-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.ts
More file actions
100 lines (76 loc) · 1.74 KB
/
basics.ts
File metadata and controls
100 lines (76 loc) · 1.74 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Primitives: numbers, strings, boolean
// More complex types: arrays, objects
// Function types, parameters
// Primitives
let age: number = 24;
age = 12;
let userName: string;
userName = 'Max';
let isInstructor: boolean;
isInstructor = true;
// More complex types
let hobbies: string[];
hobbies = ['Sports', 'Cooking'];
type Person = {
name: string;
age: number;
};
let person: Person
person = {
name: 'Max',
age: 32
}
let people: Person[];
people = [person, person]
// Type inference
let course: string | number = 'React - The Complete Guide';
course = 12341;
// Functions & types
function add(a: number, b: number): number {
return a + b;
}
function printOutput(value: any) {
console.log(value);
}
// Generics
function insertAtBeginning<T>(array: T[], value: T) {
const newArray = [value, ...array];
return newArray
}
const demoArray = [1, 2, 3]
const updatedArray = insertAtBeginning(demoArray, -1) // [-1, 1, 2, 3]
const stringArray = insertAtBeginning(['a', 'b', 'c'], 'd');
class Student {
// firstName: string;
// lastName: string;
// age: number;
// private courses: string[];
constructor(
public firstName: string,
public lastName: string,
public age: number,
private courses: string[]
) { }
enroll(courseName: string) {
this.courses.push(courseName);
}
listCourses() {
return this.courses.slice();
}
}
const student = new Student("Cam", "Kaz", 34, ["Writing 101", "Mathematics"]);
student.firstName;
student.enroll('Angular');
interface Human {
firstName: string;
age: number;
greet: () => void;
}
let max: Human;
max = {
firstName: 'Max',
age: 32,
greet() {
console.log("Hey y'all")
}
}