O'Reilly logo

Learning Angular for .NET Developers by Rajesh Gunasundaram

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generics

Generics come in very handy when developing reusable components that can work against any data type. So, the client that consumes this component will decide what type of data it should act upon. Let's create a simple function that returns whatever data is passed to it:

function returnNumberReceived(arg: number): number {     return arg; } function returnStringReceived(arg: string): string {     return arg; } 

As you can see, we need individual methods to process each data type. We can implement the same in a single function using the any data type, as follows:

function returnAnythingReceived (arg: any): any {     return arg; } 

This is similar to generics. However, we don't have control over the return type. If we pass a number and we can't ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required