Microsoft/TypeScript

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

Open

#42,283 opened on Jan 11, 2021

View on GitHub
ย (4 comments)ย (0 reactions)ย (0 assignees)TypeScriptย (48,455 stars)ย (6,726 forks)batch import
BugDomain: JSDocHelp Wanted

Description

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

Contributor guide

JSDoc `@template` generic with promise not resolved with CommonJS Export ยท Microsoft/TypeScript#42283 | Good First Issue