-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.ts
102 lines (86 loc) · 2.1 KB
/
variables.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
// variable declarations
// FORMAT: let variable: type = value;
let myname: string = "Tomiwa";
let age: number = 10;
let winner: boolean = true;
let sum: number = age + age;
// ARRAY
// FORMAT: let arrayName: type[] = [];
let names: string[] = [];
names.push("John")
names.push("Tomiwa")
names.push("BlackDev")
console.log(names);
// TUPLE - is a special structure in TS that can hold as many
// -values as needed
// FORMAT: let tupleName: [type, type, type] = [];
let person: [string, number, boolean] = ["Tobi", 19, true];
// FUNCTIONS
function mySum(a: number, b: number): number {
return a+b
}
let result: number;
function calc(a: number, b:number): void{
result = a+b;
}
// optional parameter- to declare a function with optional
// parameter you use "?"
function speak(name: string, age?:number): string{
return name + " " + age;
}
// CLASSES
class Person{
fullname: string;
constructor(firstname: string, lastname: string){
this.fullname = firstname + " " + lastname;
}
getName(): string {
return this.fullname
}
}
let person1 = new Person("Adetomiwa", "Adesanya");
//INTERFACES
// interfaces are just similar to struct in solidity
// FORMAT
/* interface interfaceName{
* property: type;
* }
*/
// interfaces define the contract that other classes
// or objects must comply with if implementing that interface
interface PersonInterface{
name: string;
age: number;
good: boolean;
}
function infor(person: PersonInterface): number{
return person.age;
}
class Student implements PersonInterface{
name: string;
age: number;
good: boolean;
constructor(a: string, b: number, c: boolean){
this.name = a;
this.age = b;
this.good = c;
}
}
// EXAMPLE
interface Panthera {
roar: string;
}
class Lion implements Panthera {
roar: string;
constructor(){
this.roar = 'roaarrrrr';
}
}
class Tiger implements Panthera {
roar: string;
constructor(){
this.roar = 'ROAARRRRRRR'
}
}
//Tiger and Lion implement the panthera interface, which mean
// Tiger and Lion must have a roar property