EN
TypeScript - object types
1 points
In TypeScript there are available following object types.
xxxxxxxxxx
1
let a = [ 1, 2, 3 ];
2
let b : number[] = [ 1, 2, 3 ];
3
let c : Array<number> = [ 1, 2, 3 ];
4
let d : (number|string)[] = [ 1, 2, 'text' ];
5
let e : Array<number|string> = [ 1, 2, 'text' ];
6
7
console.log(a);
8
console.log(b);
9
console.log(c);
10
console.log(d);
11
console.log(e);
Output:
xxxxxxxxxx
1
[ 1, 2, 3 ]
2
[ 1, 2, 3 ]
3
[ 1, 2, 3 ]
4
[ 1, 2, 'text' ]
5
[ 1, 2, 'text' ]
xxxxxxxxxx
1
let a = [ 1, 'text' ];
2
let b : [number, string] = [ 1, 'text' ];
3
4
console.log(a);
5
console.log(b);
Output:
xxxxxxxxxx
1
[ 1, 'text' ]
2
[ 1, 'text' ]
xxxxxxxxxx
1
interface IOperator {
2
make(a : number, b : number) : number;
3
}
4
5
class Summator implements IOperator {
6
public make(a : number, b : number) : number {
7
return a + b;
8
}
9
}
10
11
let summator = new Summator();
12
//let summator : IOperator = new Summator();
13
//let summator : Summator = new Summator();
14
15
let a = summator.make(1, 2);
16
let b = summator.make(3, 4);
17
18
console.log(`1 + 2 = ${a}`);
19
console.log(`3 + 4 = ${b}`);
Output:
xxxxxxxxxx
1
1 + 2 = 3
2
3 + 4 = 7
xxxxxxxxxx
1
class Person {
2
public constructor(private name : string, private age : number) {
3
// nothing here...
4
}
5
6
public print() : void {
7
console.log(`name: ${this.name}, age: ${this.age}`);
8
}
9
}
10
11
let a = new Person('John', 43);
12
let b = new Person('Kate', 21);
13
14
a.print();
15
b.print();
Output:
xxxxxxxxxx
1
name: John, age: 43
2
name: Kate, age: 21
xxxxxxxxxx
1
enum Role {
2
Admin,
3
Moderator,
4
User
5
}
6
7
enum Size {
8
Samll = 10,
9
Mmedium = 20,
10
Big = 30
11
}
12
13
enum Planet {
14
Earth = 'Earth',
15
Mars = 'Mars',
16
Jupiter = 'Jupiter'
17
}
18
19
let a = Role.Moderator;
20
let b : Role = Role.User;
21
22
let c : number = Size.Mmedium;
23
let d : Size = Size.Big;
24
25
let e : string = Planet.Mars;
26
let f : Planet = Planet.Jupiter;
27
28
console.log(Role.Admin);
29
console.log(a);
30
console.log(b);
31
32
console.log(Size.Samll);
33
console.log(c);
34
console.log(d);
35
36
console.log(Planet.Earth);
37
console.log(e);
38
console.log(f);
Output:
xxxxxxxxxx
1
0
2
1
3
2
4
10
5
20
6
30
7
Earth
8
Mars
9
Jupiter
xxxxxxxxxx
1
let f1 = function() { console.log('Hello world!'); };
2
let f2 = function() : void { console.log('Hello world!'); };
3
let f3 = () => console.log('Hello world!');
4
let f4 = () : void => console.log('Hello world!');
5
let f5 = new Function('console.log(\'Hello world!\')');
6
7
let f6 : Function = function() { console.log('Hello world!'); };
8
let f7 : Function = function() : void { console.log('Hello world!'); };
9
let f8 : Function = () => console.log('Hello world!');
10
let f9 : Function = () : void => console.log('Hello world!');
11
let f10 : Function = new Function('console.log(\'Hello world!\')');
12
13
f1(); f2(); f3(); f4(); f5();
14
f6(); f7(); f8(); f9(); f10();
Output:
xxxxxxxxxx
1
Hello world!
2
Hello world!
3
Hello world!
4
Hello world!
5
Hello world!
6
Hello world!
7
Hello world!
8
Hello world!
9
Hello world!
10
Hello world!