typescript Generics

Generic: parameters of the type generally used to restrict the set of content
class Person {
    constructor(public name:string) {
    }
    eat() {
        console.log(this.name)
    }
}
var workers: Array<Person> = [];

Where <Person> is the array of generics, which he defines this array can only put a Person

 

Workers [0] = new new the Person ( 'zhangsan'); // possible 
Workers [. 1] = 2; // this time on the error
Inside a generic statement, the array can only put inside the Person type of data. This is generic, indicating that only put a certain type of element

 

Guess you like

Origin www.cnblogs.com/wzndkj/p/11665380.html