-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
174 lines (135 loc) · 3.2 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
let firstName: string = "obiora"
firstName = "mark"
console.log(firstName);
//tuple
let fruits: [number, string, boolean]
fruits = [1, 'mango', true]
fruits = [2, 'pawpaw', false]
//tuple array
let employee: [number, string][]
employee = [
[1, 'obiora'],
[2, 'mark'],
[3, 'linus']
]
//union : you can use type in union
let pid: string | number
pid = 1
pid = "4"
//enums
enum direction1{
up,
down,
left,
right
}
enum direction2{
up = 1,
down= 2,
left = 3,
right = 4
}
enum direction3{
up = 'up',
down = 'down',
left = 'left',
right = 'right'
}
//object
const user: {
id: number,
firstName: string,
} = {
id: 1,
firstName: 'obiora'
}
// or you can declare it using type, u can use type in union too;
type user = {
readonly id: number, // using readonly, this way you can only read the value without changing the value
firstName: string
age?: number // using optional to decelare type
}
const user2: user = {
id: 1,
firstName: 'obiora'
}
//type assertion/ casting
let cid: any = "mark"
let customerID = <number>cid
customerID = 5
// OR
let uid: any = "mark"
let userID = uid as number
customerID = 5
//functions
function addNum(x: number, y: number): number{
return x + y
}
console.log(addNum(3, 6));
//function without return
function info(message: number | string): void {
console.log(message)
}
info("hello world")
//interface : note that you cant use interface in unio but you can use type
interface userInterface {
id: number,
firstName: string
}
const user3: userInterface = {
id: 1,
firstName: 'obiora'
}
//using interface with function
interface mathFunc{
(x: number, y: number): number
}
const add: mathFunc = (x: number, y: number): number => x + y;
interface personInterface {
id: number,
firstName: string
register(): string
}
//classes
class person {
private id: number // using private, public and protected properties to decide how an element can be accessed globally or locally
name: string
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
register() {
return `${this.name} is now registered`; // a method
}
}
const person1 = new person(1, 'linus mark')
const person2 = new person(2, 'amaka')
console.log(person1, person2);
console.log(person1.register());
//generic: this helps us use placeholders, use this instead of using type any
function getArrary<T>(items: T[]): T[] {
return new Array().concat(items)
}
let numArray = getArrary<number>([1, 2, 3, 4])
let strArray = getArrary<string>(['mark', 'john', 'Bob'])
numArray.push(2)
strArray.push('amaka')
// function that detects an even number
function evenNumber(x: number): boolean {
if (x % 2 === 0) {
return true
}
return false
}
console.log(evenNumber(10))
//function that detects a palindrome
function palindrome(words: string): boolean {
const cleanedWords:string = words.replace(/[\W_]/g, "").toLowerCase();
const reversedWords = cleanedWords.split("").reverse().join("");
if (cleanedWords === reversedWords) {
return true;
} else {
return false;
}
}
console.log(palindrome("amma"))