Accepted kinds of data structure for generic type

The concept of generic spreads beyond just interfaces. Generic is available for types but also with classes, and it can transform a function as well. The disposition of the brackets that define the generic type goes right after the interface name, type, or the class name. We will see later that it must also go after the name of a function. Generic can be used to have a generic field, generic parameter, generic return type, and generic variable:

type MyTypeA<T> = T | string; // Type

interface MyInterface<TField, YField> { // Interface wiht two types
entity1: TField;
myFunction(): YField;
}

class MyClass<T>{ // Class
list: T[] = [];
public displayFirst(): void {
const first: T = this.list[0]; // Variable
console.log(first);
}
}

A generic type can have many generics at the same time, allowing multiple fields or function parameters to have a different kind of type:

function extractFirstElement<T, R>(list: T[], param2: R): T {
console.log(param2);
return list[0];
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167