TypeScript generates new instances through generics

                       TypeScript generates new instances through generics


table of Contents

1. Blog introduction

2. Content

3. Push

4. Conclusion


1. Blog introduction

This article records knowledge points, TS uses generics to generate new instances


2. Content

class Person {
    firstName = 'John';
    lastName = 'Doe';
}

class Factory {
    create<T>(type: (new () => T)): T {
        return new type();
    }
}

let factory = new Factory();
let person = factory.create(Person);

3. Push

Github:https://github.com/KingSun5


4. Conclusion

        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

 

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/102752139