TS Confuses namespace with the class on JSDoc module.exported class with static method.

Abierto
#55,875 1 comentario 4 reacciones 1 asignado Ver en GitHub

@sandersn ya está trabajando en esto.

Desde el 4/10/2023.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

Bug Domain: JSDoc Help Wanted
🔎 Search Terms

"module.exports static method" "namespace vs class confusion" "namespace vs class collision" "Two different types with this name exist, but they are unrelated."

🕗 Version & Regression Information

Versions tried:

  • Version 5.3.0-dev (55d8bed85c11672b2b6f9ee3e6e1d384bef6f4da - )
  • Version 5.2.2
  • Version 5.1.6
  • Version 5.0.4
  • Version 4.9.5
⏯ Playground Link

https://www.typescriptlang.org/dev/bug-workbench/?noUnusedLocals=true&noUnusedParameters=true&checkJs=true&allowJs=true&module=1&emitDeclarationOnly=true&stripInternal=true#code/PTAEAEFsHsBMFcA2BTAXKAxtSMB2ArAZwCgQIYEUAlZQ6ReAFwEtpd0A5OZUscWZBkQBDAE7CWbXhGSRmjACKCR4ybgDyuRAE9p4YYkTQA7gCkSZcBgAWggNbm9jMQHNkjUACIAogGUATAAMQZ56iMwARqAA2j4BwYGeALp6hIyizAAOAJK4jMiiuAZ6uNDZkJnhGPIAKtbMFnylAKq48ITIsAAy0BgGjRAtbR2wAApiwpDuBSR6AGbMKEVT6MvIAHRExADk7cigaRkYjNsA3MSkAFSXxKCXoOqZaoSgc9CioIy2oLjuwrCwUS0QjrW73cCMbSZTrIOagADe6gi+EEjAAvqAOJNkI9nmCIJlRNBoaJIQjfOlmLgXBi1mDgBdgNd8Rx3KAAIIAoGEEh3UA0Qm0ZB5F7CH7uYzvOygf6A4Ggu4M4hCYQ8zHYhG3UBM8GQ6HkynUjGXBmgH7Y85anVavkAYSBEn2Yt+jElomlsu5IJtJouZqwuEO8GO7wAFABKTVms1fBrrNagAC8oG2+TSZy1aL92uZ0fuuRRx1AhOJBRYtFeRMgoGs0DSABpi+9GI3hLhYOLXVKFWbwZkJtX4RSMkbzVMfRAgYx4IUXvDY4Qs3mlWa5lWsVNQ2tI-CbZ96iCE8m1udo6ApzPcPuGqfQFmrbnewSBwaRzSxzw85P3Je5xvkEuvYrgczgsBglbYP+W7Yjue4XoU4rGNehARusa6Qdi0FTOGt5ZveTL4t4AAemR1p+vrEBQSAbMgJHNi8yb-palgLEs2LoO0whuJsJC7B0IFHCcloBmk6pTEm57IAAjvAzBAqG2zrMAazbDhjLXBAer7PC-7GgyKAeJ6wKWkZarHsgSFQWpxCmYeGrJtsdBTCpzF8KxwrscWwgYHYXEbEQUjwp4hLMAAbo6njoOk8AAUAA

💻 Code
// @filename: name.js
'use strict';

/**
 * Options for the netaddress.
 * @typedef {Object} NameOptions
 * @property {String} name
 */

/**
 * Net Address
 * Represents a network address.
 */

class Name {
  /** @type {String} */
  name;

  /**
   * Create a network address.
   */

  constructor() {
    this.name = 'test';
  }

  /**
   * Inject properties from host, port, and network.
   * @param {String} name
   * @returns {this}
   */

  fromName(name) {
    this.name = name;
    return this;
  }

  /**
   * @param {String} name
   * @returns {Name}
   */

  static fromName(name) {
    return new this().fromName(name);
  }
}

/*
 * Expose
 */

module.exports = Name;
// @filename: usage.js
'use strict';

const Name = require('./name');

/** @type {Name} */
let address;

address = new Name();

address.name = 'somename';
🙁 Actual behavior

This reports an error:

Type 'import("/name")' is not assignable to type 'import("/name")'. Two different types with this name exist, but they are unrelated.
  The types returned by 'fromName(...)' are incompatible between these types.
    Type 'Name' is not assignable to type 'this'.
      'Name' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Name'.
🙂 Expected behavior

new Name() and @type {Name} should be the same as module.exports = Name class.

Additional information about the issue

Repository format: https://github.com/nodech/type-tsjs-tests

generated declaration file for name.js is in the form:

export = Name;

declare class Name {...}
declare namespace Name { export { NameOptions } }
type NameOptions

If you remove @typedef {Object} NameOptions from the name.js, the declaration becomes

export = Name;

declare class Name {...`

and issue goes away.

More confusing

If you leave NameOptions but remove static member static fromName of the class Name, it still has namespace and class with the same name, BUT issue goes away ?

Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/TypeScript

Todos los issues de microsoft/TypeScript

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.