-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_types.ts
186 lines (156 loc) · 4.46 KB
/
data_types.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
182
183
184
185
186
// Working with Types
//number, string, boolean, object, array, tuple, enum, any
function add(n1: number, n2: number) {
return n1 + n2;
}
const number1 = 5;
const number2 = 2.8;
console.log(add(number1, number2));
//objects
const person: {
name: string;
age: number;
} = {
name: "Asttle",
age: 25,
};
console.log(person.name);
// array
const employee_details: {
name: string;
age: number;
hobbies: string[];
} = {
name: "Asttle",
age: 30,
hobbies: ["games", "music"],
};
console.log(employee_details.hobbies);
//Tuples
// setting array with restricted number of elements
const business: {
business_name: string;
role: [number, string];
} = {
business_name: "ABC",
role: [1, "Developer"],
};
business.role.push("HR"); // exception it should not be but it allows. Atleast it restricts type
console.log(business.role);
//ENUM
enum Role {
ADMIN,
DEVELOPER,
NORMAL_USER,
} // by default it assigns value from 0 and increments
enum ValueAssign {
value1 = 5,
value2 = 20,
value3 = 30,
} // we can define customvalues
enum DiffTypes {
name = "Asttle",
age = 25,
} // we can define values of different types
//any
let favorites: any[]; // it can be of any type
// maximum try to avoid this
//Union Types
// pass 2 types of inputs for functions
//function that adds if number or concats if string
function combine(n1: number | string, n2: number | string) {
let result;
if (typeof n1 === "number" && typeof n2 === "number") {
result = n1 + n2;
} else {
result = n1.toString() + n2.toString();
}
return result;
}
const combineages = combine(30, 26);
const combinenames = combine("Max", "millian");
//Literal types
// for an argument if you want to allow only n values for a type then we can use literals
function combineAnyData(
n1: number | string,
n2: number | string,
resultType: "as-number" | "as-text"
) {
let result;
if (
(typeof n1 === "number" && typeof n2 === "number") ||
resultType === "as-number"
) {
result = +n1 + +n2;
} else {
result = n1.toString() + n2.toString();
}
return result;
}
const combinenumber = combineAnyData(30, 20, "as-number");
const combineStringwithnumbervalues = combineAnyData("30", "20", "as-number");
const combineText = combineAnyData("Asttle", "Joseph", "as-text");
//TypeAliases
type Combineable = number | string;
type TypeDescriptor = "as-number" | "as-text";
function combineWithAlias(
n1: Combineable,
n2: Combineable,
resultType: TypeDescriptor
) {
let result;
if (
(typeof n1 === "number" && typeof n2 === "number") ||
resultType === "as-number"
) {
result = +n1 + +n2;
} else {
result = n1.toString() + n2.toString();
}
return result;
}
// function return types
// explicitly mention the return type of the function
function addition(n1: number, n2: number): number {
return n1 + n2;
}
// by default if function doent return anything it will considered void
function printResult(result: number) {
console.log("Result", result);
}
// Function as Types
let addVariable;
addVariable = addition;
console.log(addVariable(5, 10)); // problem is addVariable is any type so in future if addVariable is assigned another value we will get runtime error
let addFunction: Function;
addFunction = addition;
console.log(addVariable(5, 10)); // eventhough it restricts to function we can define any function to addFunction. So we will not get desired result and desired function executed
let addFunctionType: (a: number, b: number) => number; // we can assign a function which accpets 2 numbers and return a numnber
addFunctionType = addition;
//Function Types and callbacks
function addAndHandleCallback(
n1: number,
n2: number,
cb: (num: number) => void
) {
const result = n1 + n2;
cb(result);
}
addAndHandleCallback(20, 30, (result) => {
console.log(result);
});
// unknown type
let userInput: unknown;
let userName: string;
userInput = "Asttle";
//even though userinput holds a string it cannot be assigned to username.t is a strict version of any type
// so we need to typecheck before assigning, so only unkown is recommended for any
if(typeof userInput === 'string')
userName = userInput;
//never type
//common use case of never type is an error function
function generateError(message: string, code: number): never {
throw ({message: message, errorCode: code});
}
generateError('Something went wrong!', 500);
// console.log('crashed') // this console log will never be executed because app will crash when throw error. So assign never to function to clarify it