-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.ts
More file actions
66 lines (48 loc) · 1.71 KB
/
array.ts
File metadata and controls
66 lines (48 loc) · 1.71 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
// Moving on to arrays. We can declare an array of a certain type by using the following syntax:
// let arrayName: type[] = [value1, value2, value3, ...]
const languages = []
// Now let's try to add some values to the array
// languages.push('JavaScript') //It's throwing an error because we haven't declared the type of the array
// Let's add the type of the array to fix this error and the type is string
const languages2: string[] = []
// Now let's try to add some values to the array
languages2.push('JavaScript') // It's working fine now
console.log(languages2)
/*
Here is the result:
[ 'JavaScript' ]
*/
// If we try to add a number to the array it will throw an error
// languages2.push(123) // This will throw an error because we have declared the type of the array as string
// To add multiple types to an array we can use the following syntax:
const languages3: (string | number)[] = []
// That means we can add string or number to the array
languages3.push('JavaScript') // It's working fine now
languages3.push(123) // It's also fine now
console.log(languages3)
/*
Here is the result:
[ 'JavaScript', 123 ]
*/
// We can also use type to declare an array of objects
type arrayUser = {
_id: string;
name: string;
age: number;
email: string;
isActive?: boolean; // We can also use optional variable here
}
const users: arrayUser[] = []
users.push({_id: '123', name:'Ashiq', age: 18, email: 'ashiqtasdid5@gmail.com'}) // We don't have to pass the isActive variable because it's optional
console.log(users)
/*
Here is the result:
[
{
_id: '123',
name: 'Ashiq',
age: 18,
email: 'ashiqtasdid5@gmail.com'
}
]
*/