在 Typescript JSDoc 中约束泛型

spi*_*ech 4 jsdoc typescript

我正在使用 TypeScript 的 JSDoc 形式,并尝试使用扩展对象的泛型。我的编辑器给了我一个 TypeScript 错误,用于index.js声明 type 参数的代码MyInterface<T>,说Type 'T' does not satisfy the constraint '{ a: number; }'.

如何指定我接受一个通用参数来约束 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);
}
Run Code Online (Sandbox Code Playgroud)

EEC*_*LOR 6

/**
 * @template {{a: number}} T
 * @param {MyInterface<T>} impl
 */
Run Code Online (Sandbox Code Playgroud)

@template拉取请求 24600中实施了约束