microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.1k stars 12.37k forks source link

JSDocs comments being removed while autogenerating a .d.ts file for a const JavaScript namespace #46010

Open PSYmoom opened 2 years ago

PSYmoom commented 2 years ago

Bug Report

I am currently trying to autogenerate .d.ts files for my JavaScript API using --declaration and I am running into problems. All of the files which use a const namespace will have all of their comments removed, including the JSDocs ones. I have set removeComments as false in tsconfig and sanity checked it by a class with comments and all of the comments were retained in the class, but not the namespace.

πŸ”Ž Search Terms

Duplicate methods --declaration namespace .d.ts bug

πŸ•— Version & Regression Information

4.4.3 but happens with any 4.x.xversion I've tried (haven't tried earlier versions).

⏯ Playground Link

The Playground was not playing nicely with emitDeclaration and filenames with .js extension

πŸ’» Code

/**
 * Foo namespace
 * @namespace
 */
const Foo = {
    /** Test function */
    foo: function() {},
    /** Test property */
    bar: 1
}

πŸ™ Actual behavior

declare namespace Foo {
    function foo(): void;
    const bar: number;
}

πŸ™‚ Expected behavior

 /**
  * Foo namespace
  * @namespace
  */
 declare namespace Foo {
    /** Test function */
    function foo(): void;
    /** Test property */
    const bar: number;
 }
sanjeetsuhag commented 2 years ago

We're running into this issue too. A simple enum example


/**
 * The direction enum.
 * 
 * @enum {number}
 */
enum Direction {
  /**
   * Up direction.
   * 
   * @type {number}
   */
  Up = 1,
  /**
   * Down direction.
   * 
   * @type {number}
   */
  Down = 2,
  /**
   * Left direction.
   * 
   * @type {number}
   */
  Left = 3,
  /**
   * Right direction.
   * 
   * @type {number}
   */
  Right = 4,
}
export default Direction;

generates the following output:

export default Direction;
/**
 * The direction enum.
 */
type Direction = number;
declare namespace Direction {
    const Up: number;
    const Down: number;
    const Left: number;
    const Right: number;
}

TS Playground link.

Artxe2 commented 11 months ago

This problem is still occurring. Is there no work scheduled for this?

// js source

/**
 * default
 * @param {number} param
 */
export default param => {
  /**
   * default.result
   * @param {string} p
   */
  const result = p => p + p

  /**
   * default.result.fn
   */
  result.fn = () => {}
  return result
}
// generated d.ts

declare function _default(param: number): {
    (p: string): string;
    /**
     * default.result.fn
     */
    fn(): void;
};
export default _default;

Playground