Microsoft/TypeScript

JSDoc `@template` generic with promise not resolved with CommonJS Export

Open

#42,283 建立於 2021年1月11日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)TypeScript (48,455 star) (6,726 fork)batch import
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

Related issues

貢獻者指南