1
BY SAIKAT SAMANTA.
2
What is Generic Programming?
Generic programming is a style of computer programming in
which algorithms are written in terms of types to-be-specified-
later that are then instantiated when needed for specific types
provided as parameters.
There are many popular programming languages like, java, c++,
c# supported this programing style.
3
Why Should We use Generic?
Generic allow you to reuse code with different data types
and improve maintainability.
Stronger type checks at compile time and Fixing compile-time
errors is easier than fixing runtime errors.
Let's Give a deeper look...
4
Example(In Typescript):
 function functionName(args: number): number {
return args;
}
 Here we can pass an argument(only number) through the above function,
and it's return the same data type(only number).
How can we use this function for other data type?
5
Let's Use The Function for String:
function functionName(args: string): string{
return args;
}
 To use the same function for "string" data-type we have
to write the function again in this way. But this is void
the DRY (do not repeat yourself ) principle.
6
 function functionName(args: any): any {
return args;
}
 Now we can pass any data type as an argument in above function, and it's
return any data type, but here's the problem again.
Here's The Better Solution:
Because there is no guaranty to return the required data type.
7
Generic Approach:
Here's the generic style approach to solve the problem:
üfunction functionName<T>(args: T): T {
return args;
}
functionName<string>("hello"); // output: "hello"
functionName<number>(10); // output: 10
So, now we've added a type variable "T" to the function. This T allows us to
capture the type the user provides (e.g. number), and we use that "T" as
our argument type and return type to maintain a particular data type.
8
Uses Of TS generics:
We can use TS Generics as,
1. Generic Function
2. Generic Class
3. Generic Interface
4. Generic type, and
5. Generic Constrain
9
Generic Function in Typescript:
Standard function:
function functionName(args: any): any {
return args;
}
In Generic Way:
function functionName<T>(args: T): T {
return args;
}
10
Generic Class in Typescript:
Standard class:
class stdClass {
private datas: any[ ] = [ ];
// get value
public getLastData(): any{
return this.datas.pop();
}
// set value
public addData(item: any): void {
this.datas.push(item);
}
}
In Generic Way:
class stdClass <T>{
private datas: T[ ] = [ ];
// get value
public getLastData(): T{
return this.datas.pop();
}
// set value
public addData(item: T): void {
this.datas.push(item);
}
}
11
Multiple Type Variable:
In Generic Way:
class stdClass <T, U>{
private datas: T[ ] = [ ];
private values: U[ ] = [ ];
// get value
public getLastData(): T{
return (`${this.datas.pop()} and ${this.values.pop()}`) ;
}
// set value
public addData(item1: T, item2: U): void {
this.datas.push(item1);
this.values.push(item2);
}
}
How to use the Generic class:
const std1 = new stdClass<string,
number>( );
// Adding data to the array
std1.addData(“Jhon”, 60);
// Getting data from the array
std1. getLastData();
12
What exactly does a generic type represent?
Suppose you want to create a function that will take an argument of any type and
return a value of that type. Perhaps the solution that comes to mind is the following:
function doSomething(arg: any): any {…}
Correct Solution:
function doSomething<T>(arg: T): T {...}
13
Generic types
14
15
16
Generic Interface
17
Generic Constraints
• Instead of working with any and all types, we’d like to constrain this function to
work with any and all types that also have the .firstName and .lastName
property.
• As long as the type has this member, we’ll allow it, but it’s required to have at
least this member. To do so, we must list our requirement as a constraint on
what T can be.
18
19
Conclusion
• Use generics when you are willing to accept any type.
• Once defined your code accepts to work only with it.
20
Question?
21
Thank You…

A Details Overview How to Use Typescript Generic Type

  • 1.
  • 2.
    2 What is GenericProgramming? Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified- later that are then instantiated when needed for specific types provided as parameters. There are many popular programming languages like, java, c++, c# supported this programing style.
  • 3.
    3 Why Should Weuse Generic? Generic allow you to reuse code with different data types and improve maintainability. Stronger type checks at compile time and Fixing compile-time errors is easier than fixing runtime errors. Let's Give a deeper look...
  • 4.
    4 Example(In Typescript):  functionfunctionName(args: number): number { return args; }  Here we can pass an argument(only number) through the above function, and it's return the same data type(only number). How can we use this function for other data type?
  • 5.
    5 Let's Use TheFunction for String: function functionName(args: string): string{ return args; }  To use the same function for "string" data-type we have to write the function again in this way. But this is void the DRY (do not repeat yourself ) principle.
  • 6.
    6  function functionName(args:any): any { return args; }  Now we can pass any data type as an argument in above function, and it's return any data type, but here's the problem again. Here's The Better Solution: Because there is no guaranty to return the required data type.
  • 7.
    7 Generic Approach: Here's thegeneric style approach to solve the problem: üfunction functionName<T>(args: T): T { return args; } functionName<string>("hello"); // output: "hello" functionName<number>(10); // output: 10 So, now we've added a type variable "T" to the function. This T allows us to capture the type the user provides (e.g. number), and we use that "T" as our argument type and return type to maintain a particular data type.
  • 8.
    8 Uses Of TSgenerics: We can use TS Generics as, 1. Generic Function 2. Generic Class 3. Generic Interface 4. Generic type, and 5. Generic Constrain
  • 9.
    9 Generic Function inTypescript: Standard function: function functionName(args: any): any { return args; } In Generic Way: function functionName<T>(args: T): T { return args; }
  • 10.
    10 Generic Class inTypescript: Standard class: class stdClass { private datas: any[ ] = [ ]; // get value public getLastData(): any{ return this.datas.pop(); } // set value public addData(item: any): void { this.datas.push(item); } } In Generic Way: class stdClass <T>{ private datas: T[ ] = [ ]; // get value public getLastData(): T{ return this.datas.pop(); } // set value public addData(item: T): void { this.datas.push(item); } }
  • 11.
    11 Multiple Type Variable: InGeneric Way: class stdClass <T, U>{ private datas: T[ ] = [ ]; private values: U[ ] = [ ]; // get value public getLastData(): T{ return (`${this.datas.pop()} and ${this.values.pop()}`) ; } // set value public addData(item1: T, item2: U): void { this.datas.push(item1); this.values.push(item2); } } How to use the Generic class: const std1 = new stdClass<string, number>( ); // Adding data to the array std1.addData(“Jhon”, 60); // Getting data from the array std1. getLastData();
  • 12.
    12 What exactly doesa generic type represent? Suppose you want to create a function that will take an argument of any type and return a value of that type. Perhaps the solution that comes to mind is the following: function doSomething(arg: any): any {…} Correct Solution: function doSomething<T>(arg: T): T {...}
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
    17 Generic Constraints • Insteadof working with any and all types, we’d like to constrain this function to work with any and all types that also have the .firstName and .lastName property. • As long as the type has this member, we’ll allow it, but it’s required to have at least this member. To do so, we must list our requirement as a constraint on what T can be.
  • 18.
  • 19.
    19 Conclusion • Use genericswhen you are willing to accept any type. • Once defined your code accepts to work only with it.
  • 20.
  • 21.