-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescriptplay.ts
137 lines (113 loc) · 2.41 KB
/
typescriptplay.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
var message:string ='hello world';
console.log(message);
/*
Type Assertion. Kind of type casting in C++/JAVA
*/
var str = '100';
var num:number = <number> <any> str;
console.log(num);
/*
functions with Rest parameters
*/
function addsum(...nums:number[]){
var sum = 0;
for (var i = 0; i < nums.length; i++) {
sum+=nums[i];
}
console.log(sum);
}
addsum(1,2,3,4);
/*
function overloading
*/
function disp(s1:string):void;
function disp(n1:number,s1:string):void;
function disp(x:any,y?:any):void {
console.log(x);
if(y!=undefined)
console.log(y);
}
disp("abc");
disp(1,"xyz");
/*
Tuples
*/
var myTuple = [10,'hello','world'];
console.log(myTuple[1]);
/*
Union Type
*/
var num2:number|string;
num2 = 1;
console.log(num2);
num2 = 'hello';
console.log(num2);
//num2 = true; throws error because num2 is not of Boolean type
/*
interfaces
*/
interface IPerson {
firstName:string,
lastName:string,
sayHi: ()=>string
}
var customer:IPerson = {
firstName:"Quentin",
lastName:"Tarantino",
sayHi: ():string =>{return "Hi there"}
}
console.log(customer.firstName)
console.log(customer.lastName)
console.log(customer.sayHi())
/*
class and objects
*/
class Animal{
name:string;
color : string;
constructor(name:string,color:string){
this.name = name;
this.color = color;
}
};
var obj = new Animal('Dog','White');
console.log('Animal name is : '+obj.name+' , Animal color is : '+obj.color);
/*
Access Specifiers : public,private,protected
*/
class Animall{
name:string;
private color : string;
constructor(name:string,color:string){
this.name = name;
this.color = color;
}
};
var objj = new Animall('Dog','White');
//console.log('Animal name is : '+obj.name+' , Animal color is : '+objj.color); //Error because field color is private
/*
Changes to objects
*/
var person = {
firstName:"Tom",
lastName:"Hanks",
sayHello:function() { } //Type template
}
person.sayHello = function() {
console.log("hello "+person.firstName)
}
person.sayHello();
/*
Passing objects as parameters
*/
interface IPoint {
x:number
y:number
}
function addPoints(p1:IPoint,p2:IPoint):IPoint {
var x = p1.x + p2.x
var y = p1.y + p2.y
return {x:x,y:y}
}
var newPoint = addPoints({x:3,y:4},{x:5,y:1})
console.log(newPoint);