- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 81字
- 2021-07-15 17:05:34
Generic classes
Similar to generic interfaces, we can also define generic classes. We define classes with a generic type in angle brackets (<>) as follows:
class GenericClass<T> { add: (a: T, b: T) => T; }
var myGenericClass = new GenericClass<number>(); myGenericClass.add = function(a, b) { return a + b; };
Here, the generic class is instantiated by passing the generic data type as number. So, the add function will process and add two variables of type number passed as parameters.