-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescript_Intro.ts
172 lines (140 loc) · 4.11 KB
/
typescript_Intro.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
// interface Product{
// id: string;
// name:string;
// price:number;
// image:string;
// categories:string[];// Array<string>
// }
// type Product = {
// id: string;
// name: string;
// price: number;
// image: string;
// categories: string[];// Array<string>
// }
// let products: Product[] = [];
// products.push({
// id: "lvkttlv",
// name: "Laptop",
// price: 10000,
// image: "",
// categories: ['laptop','lenovo']
// })
//@ts-ignore
// function add(a: number|string,b: number|string) : number|string
// {
// if(typeof a === 'string' && typeof b=== 'string')
// return`${a} ${b}`;
// if(typeof a === 'number' && typeof b=== 'number')
// return a+b;
// throw new Error('Invalid');
// }
//#####################################
// const add=(a: number|string,b: number|string) =>
// {
// if(typeof a === 'string' && typeof b=== 'string')
// return`${a} ${b}`;
// if(typeof a === 'number' && typeof b=== 'number')
// return a+b;
// throw new Error('Invalid');
// };
// console.log(add(3,7));
// type AddFunctionReturnType = ReturnType<typeof add>;
//#####################################
// type Shape = {
// name: 'circle' | 'rectangle' | 'square';
// }
// type Circle = Shape & {
// // name:'circle';
// radius: number;
// }
// type Rectangle = Shape & {
// // name: 'rectangle';
// height: number;
// width: number;
// }
// type Square = Shape & {
// // name: 'square';
// side: number;
// }
// // const isCircle=(shape: Circle | Rectangle | Square): shape is Circle=> typeof (shape as Circle).radius === 'number';
// const isCircle = (shape: Circle | Rectangle | Square): shape is Circle =>
// shape.name === 'circle' &&
// typeof (shape as Circle).radius === 'number';
// const isRectangle = (shape: Circle | Rectangle | Square): shape is Rectangle =>
// shape.name === 'rectangle' &&
// 'height' in shape &&
// 'width' in shape &&
// typeof shape.height === 'number' &&
// typeof shape.width === 'number';
// const isSquare = (shape: Circle | Rectangle | Square): shape is Square =>
// shape.name === 'square' &&
// 'radius' in shape &&
// typeof shape.radius === 'number';
// function getArea(shape: Circle | Rectangle | Square): number {
// if (isCircle(shape)) {
// return 3.1416 * shape.radius * shape.radius;
// }
// if (isRectangle(shape)) {
// return shape.height * shape.width;
// }
// if (isSquare(shape)) {
// return shape.side * shape.side;
// }
// console.log(getArea({ name: 'circle', side: 5 }));
// }
//#####################################
// type Shape = {
// name: 'circle';
// radius: number;
// } | {
// name: 'square';
// side: number;
// } | {
// name: 'rectangle'
// height: number;
// width: number;
// }
// const getArea = (shape: Shape): number => {
// switch (shape.name){
// case 'circle':
// return 3.1416 * shape.radius * shape.radius;
// case 'square':
// return shape.side * shape.side;
// case 'rectangle':
// return shape.width * shape.height;
// default:
// throw new Error("Invalid Shape");
// }
// }
// console.log(getArea({name: 'circle',radius:5}));
//####################################
let a: string | null | undefined;
a = undefined;
//####################################
// const add= <T,>(a:T, b:T):T =>{
// if(typeof a==='string')
// {
// return `${a} ${b}` as T;
// }
// if(typeof a==='number' && typeof b==='number')
// {
// return a+b as T;
// }
// throw new Error('Invalid Params');
// // return a+b;
// }
// add(5,'6');
//####################################
type Product = {
id: string;
name: string;
price: number;
image: string;
categories: string[];// Array<string>
}
type ProductMetaData = Pick<Product, 'name' | 'id'>;
type ProductMinimal = Omit<Product, 'image' | 'categories'>;
type Product2 = Pick<Product, 'id'> &
Partial<Pick<Product, 'name' | 'price'> &
Required<Pick<Product, 'image'>>>