简体   繁体   中英

is there a way to declare a type parameter via JSDoc syntax in javascript for typescript to understand

this document:

doesn't mention type parameters

Questions:

  • are type parameters supported via JSDoc type annotations?
  • if so, how do i do it?

Yes, you can specify types with JSDoc in JavaScript for the TypeScript compiler to use...

/** @type {Window} */
var myWin;

/** @type {Event} */
var myEvent;

/**
 * @param myStr {string} my parameter
 * @return {string} returns a string
 */
function myFunction(myStr){
    return myStr;
}

Available in TypeScript 2.3 and later.

Using generic type parameters:

/**
 * @template T
 * @param {T} thing
 * @return {T}
 */
function getThing(thing){
    return thing;
}

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