简体   繁体   中英

Factory function syntax in typescript

I don't understand the c type marker syntax here : https://visualstudiomagazine.com/blogs/tool-tracker/2015/12/factory-functions-typescript.aspx

function CreateCustomer<c extends ICustomer>(cust:{new(): c;}, 
                                         name: string, age: number): c

Can someone explain what is c above in the 3 different places ?

1st c is declaration that collection variable have to extend ICustomer (in app you can have VIPCustomer, MegaCustomer...)

Then 2nd c is just safetyguard that constructor of c is 100% extending ICustomer (returning this object into cust)

The last c is return type of the whole function.

Have a nice day!

  1. <c extends ICustomer> means " c is a type that extends ICustomer "
  2. cust:{new(): c;} means " cust is a function that can be called with new cust() and will return an object of type c "
  3. function CreateCustomer/*...*/(/*...*/): c means "function CreateCustomer returns an object of type c "

The function can be called like that:
var john:Customer = CreateCustomer(Customer, "John", 35)
Where Customer is a class extending ICustomer interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM