Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraining generics in Typescript JSDoc

I'm using the JSDoc form of TypeScript and am trying to use a generic that extends an object. My editor is giving me a TypeScript error for the index.js code declaring the parameter of typeMyInterface<T>, saying Type 'T' does not satisfy the constraint '{ a: number; }'.

How do I specify that I accept a generic parameter that constrains an object in JSDoc TypeScript?

// index.d.ts
declare interface MyInterface<T extends {a: number}> {
  b: string;
}


// index.js
/**
 * @template T
 * @param {MyInterface<T>} impl
 */
function doStuff(impl) {
  console.log(impl);
}
like image 648
spiffytech Avatar asked Mar 06 '23 19:03

spiffytech


1 Answers

/**
 * @template {{a: number}} T
 * @param {MyInterface<T>} impl
 */

@template constraints were implemented in pull request 24600

like image 162
EECOLOR Avatar answered Mar 27 '23 21:03

EECOLOR