Languages
[Edit]
EN

TypeScript - object types

1 points
Created by:
Vanessa-Drake
718

In TypeScript there are available following object types.

1. Array type example

let a = [ 1, 2, 3 ];
let b : number[] = [ 1, 2, 3 ];
let c : Array<number> = [ 1, 2, 3 ];
let d : (number|string)[] = [ 1, 2, 'text' ];
let e : Array<number|string> = [ 1, 2, 'text' ];

console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);

Output:

[ 1, 2, 3 ]
[ 1, 2, 3 ]
[ 1, 2, 3 ]
[ 1, 2, 'text' ]
[ 1, 2, 'text' ]

2. Tuple type example

let a = [ 1, 'text' ];
let b : [number, string] = [ 1, 'text' ];

console.log(a);
console.log(b);

Output:

[ 1, 'text' ]
[ 1, 'text' ]

3. Interface type example

interface IOperator {
    make(a : number, b : number) : number;
}

class Summator implements IOperator {
    public make(a : number, b : number) : number {
        return a + b;
    }
}

let summator = new Summator();
//let summator : IOperator = new Summator();
//let summator : Summator = new Summator();

let a = summator.make(1, 2);
let b = summator.make(3, 4);

console.log(`1 + 2 = ${a}`);
console.log(`3 + 4 = ${b}`);

Output:

1 + 2 = 3
3 + 4 = 7

4. Class type example

class Person {
    public constructor(private name : string, private age : number) {
        // nothing here...
    }

    public print() : void {
        console.log(`name: ${this.name}, age: ${this.age}`);
    }
}

let a = new Person('John', 43);
let b = new Person('Kate', 21);

a.print();
b.print();

Output:

name: John, age: 43
name: Kate, age: 21

5. Enum type example

enum Role {
    Admin,
    Moderator,
    User
}

enum Size {
    Samll = 10,
    Mmedium = 20,
    Big = 30
}

enum Planet {
    Earth = 'Earth',
    Mars = 'Mars',
    Jupiter = 'Jupiter'
}

let a = Role.Moderator;
let b : Role = Role.User;

let c : number = Size.Mmedium;
let d : Size = Size.Big;

let e : string = Planet.Mars;
let f : Planet = Planet.Jupiter;

console.log(Role.Admin);
console.log(a);
console.log(b);

console.log(Size.Samll);
console.log(c);
console.log(d);

console.log(Planet.Earth);
console.log(e);
console.log(f);

Output:

0
1
2
10
20
30
Earth
Mars
Jupiter

6. Function type example

let f1 = function() { console.log('Hello world!'); };
let f2 = function() : void { console.log('Hello world!'); };
let f3 = () => console.log('Hello world!');
let f4 = () : void => console.log('Hello world!');
let f5 = new Function('console.log(\'Hello world!\')');

let f6 : Function = function() { console.log('Hello world!'); };
let f7 : Function = function() : void { console.log('Hello world!'); };
let f8 : Function = () => console.log('Hello world!');
let f9 : Function = () : void => console.log('Hello world!');
let f10 : Function = new Function('console.log(\'Hello world!\')');

f1(); f2(); f3(); f4(); f5();
f6(); f7(); f8(); f9(); f10();

Output:

Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

TypeScript

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join