Repository navigation
JSDoc-generated namespaces not merged with default exportsย #64707
Description
๐ Search Terms
jsdoc, namespace, @namespace, @typedef, @callback, @memberof
๐ Version & Regression Information
This is the behavior in every version I tried (6.0.x and 7.0.x), and I reviewed the FAQ for entries about JSDoc.
โฏ Playground Link
๐ป Code
/**
* @callback MyClass.MyCallback
* @param {number} a
* @param {number} b
* @returns {Promise<void>}
*/
class MyClass {
/** @param {MyClass.MyCallback} fn */ // โ
constructor(fn) {
/** @type {MyClass.MyCallback} */ // โ
this.fn = fn;
}
}
/** @type {MyClass.MyCallback} */ // โ
var myFn;
export default MyClass; // โ comment out this line and errors disappear๐ Actual behavior
6.0.3
TypeScript reports errors (TS2694: Namespace 'MyClass' has no exported member 'MyCallback'.) on all references to MyClass.MyCallback. If default export is removed, or if the class MyClass is added to named exports with export class MyClass, the error disappears and MyClass.MyCallback is resolved.
7.0.2
TypeScript resolves MyClass.MyCallback within the original source file, but not in another file that imports MyClass through its default export:
import MyClass from './MyClass.js';
/** @type {MyClass.MyCallback} */ // โ
var myFn;We can work around that...
import MyClass from "./MyClass.js";
/** @import {MyClass as MyClassNS} from "./MyClass.js"; */
/** @type {MyClassNS.MyCallback} */
let fn;... but this feels like surprising behavior.
๐ Expected behavior
Presence of export default MyClass; should not affect whether or not TypeScript can resolve references to MyClass.MyCallback locally within a file. When another file imports the default MyClass export, that import should carry both the class and the namespace definitions.
Additional information about the issue
I realize there are some open issues about support for related JSDoc features, such as @namespace and @memberof, which (if resolved) might provide alternative solutions to this problem:
- Support @typedef as member of namespaceย #25579
- JSDoc : add support for @method (and @property)ย #28730
- [Salsa] Support jsdoc
@namespaceย #14233 - JSDoc support for @memberof and @namespaceย #36176
- JSDoc types with class / namespace not combined correctlyย #48821
- Support
~namespace qualifier in JS Doc type positionsย #47199
However, the syntax @callback MyClass.MyCallback is already supported by TypeScript, it just breaks when the class/namespace is a default export. So I think this may be a bug in the current feature implementation, which might be easier to fix than adding one of the features linked above?
Locally I've written up a failing unit test for the bug, and could submit that if it's helpful.
Activity