简体   繁体   中英

What is meaning of T = {} in typescript

I m pretty fine with typescript but never understand what this syntax mean,

function cool<T = {}>() {
           // do something
}

but what is the significance of <T = {}> , can't we simply use <T> , what benefit does it provide?

T is a generic type parameter. = in this context is used to specify a default value for T , which in this case is an empty object type. Setting a default value for a type parameter makes it optional, just like with normal parameters. So if you don't provide the type argument T on invocation it will default to {} .

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