-
Notifications
You must be signed in to change notification settings - Fork 0
/
araay.ts
143 lines (90 loc) · 3.41 KB
/
araay.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
// let new_array = [1,2,4,true]
// new_array.push(56)
// console.log(new_array);
// ts array
// let new_array2 : (string|number) []; //two datatypes can be added into array string and number
// new_array2 = [124,"karachi",332,33,"pakistan",true];
// // ts array more
// // let new_var: string | number[] | boolean [];
// // new_var = "pakistan", [1,2,4,5,5] , [true,false]; // direct accessable using just this line not above alongwith
// for(let i in new_var){
// console.log(new_var[i]);
// }
// 2 multi dimenstional array
// let var_array : (string|number)[][]
// var_array = [
// ["monday","tuesday","wednesday","thursday"],[1,2,3,4]
// ]
// for(let calendar in var_array){
// console.log(var_array[calendar]);
// }
// //incorrect type
// function fun_name(var1:string[]){
// console.log(var1[323232].length); // it should throw error since the string type is defined and number is given
// }
//spreads
// let vari1 = ["t1","t2"]
// let vari2 = [1,2,3]
// let join = [...vari1,...vari2];
// for(let i in join){
// console.log(join[i]);
// }
//spread rest parameter example 02
// function spread(vari3:string,...vari4 : string[]){
// for(let i1 of vari4){
// console.log(vari3,i1)
// }
// }
// let new_vari = ["tone","ttwo"]
// spread("lahore",...new_vari);
//array definition
// let array_type:(string|number)[]
//tuple variable
// let employee : [number , string , boolean] = [1,"string data",false]; // type defined as square brackets represents tuple
// tuple passed as rest parameter.
function func_spread(var_ano:string,var_ano2:number){
console.log(`variable1 ${var_ano}`);
console.log(`variable1 ${var_ano2}`);
}
let data1 : [number,string]=[1947,"pakistan"];
func_spread(...data1); // different type number is not assignable to string
// tuple inferences
//1,2,5,6,7,11,12,13
// const assertion
let show = ["new-name",123,true]
let show2 = ["pakistan",123,true] as const // const assertion. no more types or values are not allowed.
//union type array
let new_array : string | number[]
new_array = "ahmed",[1,2,3,4,5,6];
// for(let i of new_array){
// console.log(new_array[i]);
// }
// //2D array
let TwoDarray : number[][]
TwoDarray = [[2,4,6],[1,3,5,7],[4,3,5,6,8]];
console.log(TwoDarray);
//array memebers
const defenders = ["Clarenza", "Dina"];
// Type: string
const defender = defenders[0];
console.log(defender);
//spread example
let arrayOne = ["how","are","you!"];
let arrayTwo = [1,2,4,5];
let conJoind = [...arrayOne,...arrayTwo];
console.log(conJoind);
//tuple assignablity and accessing its members
const tupleThree : [boolean,number,string] = [false,1922,"ok"];
const toAccessAnElement :[boolean , number]= [tupleThree[1]];
const toUseAllvaluesOftupleThree:[boolean , number] = tupleThree;
//all values should be assined not desired of two or three ,
//similarly all acceession is necessary not just an element we want
// arguments 2 but parameter is 3
//tuple as rest parameter
function Student(name:string,value:[number,boolean]){
console.log(`Your name is ${name} has an id : ${value[0]}
and your identity is ${value[1]}`);
}
let newStudent :[string, [number, boolean]][] =[
["abc",[1,true]],["sameas",[2,false]],["toyou",[3,true]]];
newStudent.forEach(newStudent => Student (...newStudent));