-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
105 lines (66 loc) · 2.08 KB
/
app.ts
File metadata and controls
105 lines (66 loc) · 2.08 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
const person: {name: string, age: number} = { // Describing the type after colun(:)
name: 'shubham',
age: 40,
};
console.log(person.name);
const students: any[] = ['shubham', 'shivam', 5];
let i = 0;
const data: [string, number] = ['shubham', 19]; // Tuple is for defining data structure
do {
console.log(students[i]);
i++;
} while (i < students.length);
// Understanding Enums Typescript
enum Role { ADMIN, READ_ONLY, AUTHOR };
// 0 , 1, 2
const humans = {
name: 'shubham',
role: Role.READ_ONLY,
};
if (humans.role === Role.ADMIN) {
console.log("Is admin");
} else {
console.log('Not admin')
};
// Any type
// const students: any[] = ['shubham', 'shivam', 5];
// ------------> Union Type
function combine(input1: string | number, input2: string | number, input3: 'suvi' | 'negi') { // uinion type // literals:- input3: 'suvi' | 'negi'
let result;
if(input1 === 'number' && input2 === 'number') {
result = input1 + input2;
} else {
result = input1.toString() + input2.toString();
};
return result;
};
const combineAges = combine(30, 20, 'negi');
const combineNames = combine('Max', 'Sam', 'suvi');
// ---------------> type Alises
type Combinable = number | string;
type FixedStrings = 'Suvi' | 'negi';
const TypeAlises: Combinable = 'suvi';
// ----------------> Function return and Voids
function setReturnType(n1: number, n2: number): string {
return n1.toString() + n2.toString();
}
// Function that doesn't return anything call Void Functions
function voidFunction(num: number): void {
console.log('Result: '+ num);
};
// undefined is a type in TypeScript
// Function Types
function add(n1: number, n2: number) {
return n1 + n2;
};
const functionType: Function = voidFunction;
let addValues: (a: number, b: number) => number;
addValues = add;
// Unknown Type
let userInput: unknown;
userInput = 'suvi';
// The Never Type
function generateError(message: string, code: number): never {
throw {message: message, errorCode: code};
};
generateError('An Error Occurred! ', 500);