4 Object Types
4 Object Types
Object Types
Object Types
The object types in TypeScript are the Array, Tuple, Function, Class, and Interface.
Array
The Array type is used to store the same type of multiple values in a single variable and further
your array elements you can access using the index number.
An Array type can be defined using square brackets '[ ]' followed by elemTypeor generic array
type 'Array<elemType>' as given below:
Tuple
The Tuple type represents a JavaScript array where you can define the data type of each element
in an array.
Function
https://www.dotnettricks.com/player/typescript/typescript-fundamentals 1/2
4/4/2020 Playing | TypeScipt Fundamentals
ClassObject Types
ECMAScript 6 or ES6 provides class type to build a JavaScript application by using object-
oriented class-based approach. In TypeScript, you can compile class type down to JavaScript
standard ES5 that will work across all major browsers and platforms.
1. class Student {
2. rollNo: number;
3. name: string;
4.
5. constructor(rollNo: number, name: string) {
6. this.rollNo = rollNo;
7. this.name = name;
8. }
9. showDetails() {
10. return this.rollNo + " : " + this.name;
11. }
12. }
Interface
The interface acts as a contract between itself and any class which implements it. An interface
cannot be instantiated but it can be referenced by the class object which implements it.
1. interface IHuman {
2. firstName: string;
3. lastName: string;
4. }
5. class Employee implements IHuman {
6. constructor(public firstName: string, public lastName: string) {
7. }
8. }
https://www.dotnettricks.com/player/typescript/typescript-fundamentals 2/2