-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
181 lines (130 loc) · 2.63 KB
/
index.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Type annotations
let fullName:string = "jack";
fullName = 'john';
let num : number;
num = 1;
// Array assignments
const names:string[] = [];
names.push("hi");
// function assignments
function sum(a:number, b:number):number{
return a+b;
}
// Tuples
let address : [number, string, number];
address = [111,"delhi",31111]
// Objects with Properties
let person : {name: string};
person = {name:'john'};
// Object with Optional Properties
let anotherPerson : {name: string,age?:number};
anotherPerson = {name:'john',age:20};
anotherPerson = {name:'jack'};
// Function with Optional Properties
function add(a:string,b?:string){
return b ? a+b : a
}
console.log(add('a'))
console.log(add('a','c'))
// Interface
interface Person{
name: string;
age: number;
id: number;
}
let p:Person;
p = {name: 'jack',age:40, id:1};
interface Student{
name: string;
age: number;
rollNo : number;
}
// Union
let p1: Person | Student;
p1 = {name:'jack',age:1,rollNo:1};
p1 = {name:'john',age:1,id:1};
p1 = {name:'jack',age:2,rollNo:1,id:2}
// Intersection
let p2: Person & Student;
p2 = {name:'jack',age:1,rollNo:1, id:2};
// type aliases
type Count = string | number;
let c : Count;
c = 1.2;
c = 'one';
// c = false;
type X = string & number; // never
const n:[]=[]; // never array
// n.push('hi');
// Class
class Car{
brand;
constructor(brand){
this.brand = brand
}
getModel(){
console.log(this.brand)
}
}
let c1 = new Car('Audi');
c1.getModel();
// Public
class CarX{
constructor(public brand){
}
getModel(){
console.log(this.brand)
}
}
let c2 = new CarX('BMW');
c2.getModel();
console.log(c2.brand)
// Private
class CarY{
constructor(private brand){
}
getModel(){
console.log(this.brand)
}
}
let c3 = new CarY('Mercedez');
c3.getModel();
// console.log(c3.brand);
// Protected
class CarZ{
constructor(private brand){
}
getModel(){
console.log(this.brand)
}
}
let c4 = new CarZ('Volvo');
c4.getModel();
// console.log(c4.brand)
// Implements keyword
interface ICar{
brand: string;
model: string;
}
interface ICar2{
release: number
}
class CarC implements ICar, ICar2{
constructor(public brand: string, public model: string, public release: number){
}
}
// Generics
function gen<T>(a : T,b : T) : T[]{
return [a,b]
}
console.log(gen<string>('1','2'));
console.log(gen<number>(1,2));
console.log(gen<Array<number>>([1],[2]));
// extends in Generics
function addUser<T extends {id: string}>(user:T){
return user.id;
}
// any type
function noRestrictions(a:any,b:any): any{
return a+b;
}