Microsoft/TypeScript
在 GitHub 查看JSDoc `@template` generic with promise not resolved with CommonJS Export
Open
#42,283 创建于 2021年1月11日
BugDomain: JSDocHelp Wanted
描述
Bug Report
🔎 Search Terms
JSDoc, CommonJS, ESM, ES Modules, @template, generics, module.exports, promises
🕗 Version & Regression Information
- This is the behavior in every version I tried (and I also tried
typescript@next, and I reviewed the FAQ for entries about this
⏯ Playground Link
I guess the Playground doesn't support multiple files, which this bug is based on.
A minimal repro can be found in the Code section below (based on this repo).
💻 Code
This is based on this repo: https://github.com/karlhorky/ts-jsdoc-template-promise-commonjs-bug
// main.js
const promiseWrapperCjs = require('./promise-wrapper-cjs');
const { default: promiseWrapperEsm } = require('./promise-wrapper-esm');
const resultCjs = promiseWrapperCjs(Promise.resolve(1)); // ❌ Promise<{data: T}>
const resultEsm = promiseWrapperEsm(Promise.resolve(1)); // ✅ Promise<{data: number}>
// promise-wrapper-cjs.js
/**
* @function
* @template T
* @param {Promise<T>} promise
* @returns {Promise<{data: T}>}
*/
module.exports = function promiseWrapper(promise) {
return promise.then((data) => ({
data,
}));
};
// promise-wrapper-esm.js
/**
* @function
* @template T
* @param {Promise<T>} promise
* @returns {Promise<{data: T}>}
*/
export default function promiseWrapper(promise) {
return promise.then((data) => ({
data,
}));
}
🙁 Actual behavior
Generic type not resolved in CommonJS version, but ES Module version works
🙂 Expected behavior
Generic type resolved in both CommonJS and ES Module versions