-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypescript.txt
More file actions
165 lines (104 loc) · 3.22 KB
/
Typescript.txt
File metadata and controls
165 lines (104 loc) · 3.22 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
* A superset of javascript created by Microsoft
* Compiles to plain javascript code
* Includes many ES6 features
* Cohesion concept: Things that are related should be part of one unit
Typescript compiler:
----------------------------
* Automatically compiles .ts files to .js files.
* Will be installed with NPM
* Supports ES6 features
* Used by Angular
* We can install typescript with npm as below
npm i -g typescript
* We can run typescript file like below
tsc file-name.ts
* We can execute both TS and JS files at a time as below
tsc file-name.js | node file-name.js (OR)
tsc file-name.js && node file-name.js
*
Available Types:
---------------------------
* String
* Number
* Boolean
* Null
* Array
* Any
* Void => No return type
* Tuple
* enum Color { Reb = 0, Green = 1, Blue = 2 };
* Object[] => Array of objects
* number[] => Array of numbers
* string[] => Array of strings
* any[] => Array of any
*************************************************************************************
* let aa: number; // this is called 'type annotation'
* let a = 10;
a = 'hello'; // Gives error = here the type of 'a' is 'number'
* Declaring a type a parameter while defining the function is called "Inline annotation"
Ex 1:
let drawPoint = (point: number) => {
console.log(point);
}
drawPoint(10);
Ex 2:
let drawPoint = (point: {x: number, y: number}) => {
console.log(point);
}
drawPoint({
x: 1,
y: 5
});
Note: we can achieve Ex 2 with interface as well as below
interface Point {
x: number,
y: number
}
let drawPoint = (point: Point) => {
console.log(point);
}
drawPoint({
x: 1,
y: 5
});
Note: The first word of name of the interface should be capital (Pascal case)
* let b; // here the type of 'b' is 'any'
* Type assertion concept:
Type assertion means...
let message;
message = 'abc';
let endsWithC = (<string>message).endsWith('c');
let alternativeWay = (message as string).endsWith('c');
*
* Class example:
class Point{
// private x: number;
// private y: number;
constructor(private x?: number, private y?: number){
// this.x = x;
// this.y = y;
}
draw(){
console.log('X: '+this.x+', Y: '+this.y);
}
}
// To create an object follow as below
let point: Point = new Point(1, 2); (OR) let point = new Point(1, 2);
point.draw();
*************************************************************************************
IMP points to remember:
---------------------------------
* No need to specify 'function' keyword while creating a function
* Interfaces are purely for declarations
* To execute .ts file and .js at once, follow below command
tsc filename.ts | node filename.js (OR) tsc filename.ts && node filename.js
* tsc *.ts --target ES5 && node main.js
* Once we make a parameter optional all the other parameters on the right side parameters should also optional.
*************************************************************************************
Interview things:
-------------------------------------------------------------------------------------
* https://www.youtube.com/watch?v=TlhxEYXifDw
*************************************************************************************
Useful site:
---------------------------------
* https://www.tutorialsteacher.com/typescript/