Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

TypeScript - object types

0 contributions
1 points

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!

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ­čĹő
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more