-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypescript Basics.txt
More file actions
148 lines (90 loc) · 2.39 KB
/
Typescript Basics.txt
File metadata and controls
148 lines (90 loc) · 2.39 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Typescript Basics
TS is superset of JS. It is transcompiled into JS at build/runtime so that all of the type safety syntax is removed
Compiling .ts files
------------------------
To transpile a .ts file to a .js file we have to use the typescript compiler
tsc index.ts // if there are no errors this creates a new file in the folder called index.js
Variable Declaration
--------------------------
var x:number = 10; // declares the type number for the variable
var x:number = 10;
var fname:string = "Erin"
var lname:string = "Wooden"
var isTrue:boolean = false
var product:any = {
name: "Call of Duty",
type: "Video Game",
Manufacturer: "Treyarch"
}
var array1:Array<string> = ["Low", "Erin", "Low Jr", "Joe"]
var array2:Array<number> = [2, 5, 10]
var array3:Array<any> = [2,5,10,true,"Bob"]
Types
number
string
boolean
any
enum
Function Declaration
--------------------------
// declares a function that takes two variables as type number and returns a value of type number
function add(num1: number, num2: number):number {
return num1 + num2
}
Class Declaration
-----------------------
class Student {
constructor(id: number, name: string) {
}
}
Type Aliases
—————————
- a name for any type
- a type cannot be re-opened to add new properties like an interface which is always extendable — IMPORTANT
type IProduct = {
id: number;
name: string;
description: string;
price: number;
}
Interface Declaration
-----------------------
- another way to name an object type
- define a "contract" that our objects will follow.
- this happens solely at compile time; once the code is compiled the concept of interfaces is gone.
interface IProduct {
id: number;
name: string;
description: string;
price: number;
}
// create an instance of IProduct
var product:IProduct = {
id: 1,
name: "Call of Duty",
description: "The latest COD game",
price: 59.99
}
Generic Objects
———————
interface Row<T> {
label: string;
value: T;
disabled: boolean;
}
Generic Functions
————————
const createRow = <T,>(label: string, value: T, disabled = false) => ({
// do stuff
});
Control Flow
-----------------
Selection - Conditional Execution
if-else
switch
Iterative - Execute Many Times
for
while
Transfer - Transfer Execution From One Part To Another
break
continue