0% found this document useful (0 votes)
15 views

4 Object Types

The object types in TypeScript include arrays, tuples, functions, classes, and interfaces. Arrays allow storing multiple values of the same type and accessing elements by index. Tuples define the data type of each element in an array. Functions can specify parameter and return types. Classes define object types using the class keyword and constructors. Interfaces act as contracts that classes can implement to define non-primitive object shapes.

Uploaded by

Abhishek Dabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

4 Object Types

The object types in TypeScript include arrays, tuples, functions, classes, and interfaces. Arrays allow storing multiple values of the same type and accessing elements by index. Tuples define the data type of each element in an array. Functions can specify parameter and return types. Classes define object types using the class keyword and constructors. Interfaces act as contracts that classes can implement to define non-primitive object shapes.

Uploaded by

Abhishek Dabar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4/4/2020 Playing | TypeScipt Fundamentals

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:

1. let numberList: number[] = [1, 2, 3];


2. //OR
3. let numList: Array = [1, 2, 3];

Tuple

The Tuple type represents a JavaScript array where you can define the data type of each element
in an array.

1. let tuple: [string, number];


2. tuple = ["Gurukulsight", 1];

Function

Like JavaScript, TypeScript functions can be created both as a named function or as an


anonymous function. In TypeScript, you can add types to each of the parameters and also add a
return type to function.

1. //named function with number as return type


2. function add(x: number, y: number): number {
3. return x + y;
4. }
5. //anonymous function with number as return type
6. let sum = function (x: number, y: number): number {
7. return x + y;
8. };

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.

Hence, it can be used to represent any non-primitive JavaScript object.

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

You might also like