Skip to content

Unrelated JSDoc annotation causes class to not be assignable to itself or iterable #53967

@jakebailey

Description

@jakebailey

Bug Report

🔎 Search Terms

jsdoc annotation iterable self assignable

🕗 Version & Regression Information

  • This changed between versions 4.2 and 4.3

💻 Code

Here's a compiler repro that reliably works.

// @lib: es2017
// @allowJs: true
// @checkJs: true
// @noEmit: true

// @filename: index.js
const LazySet = require("./LazySet");

/** @type {LazySet<string>} */
const stringSet = new LazySet();

stringSet.addAll(stringSet);


// @filename: LazySet.js
// Comment out this JSDoc, and note that the errors index.js go away.
/**
 * @typedef {Object} SomeObject
 */
void 0;

/**
 * @template T
 */
class LazySet {
    /**
     * @param {Iterable<T>=} iterable
     */
    constructor(iterable) {
        /** @type {Set<T>} */
        this._set = new Set(iterable);
    }

    /**
     * @param {Iterable<T> | LazySet<T>} iterable
     * @returns {LazySet<T>}
     */
    addAll(iterable) {
        return this;
    }

    [Symbol.iterator]() {
        return this._set[Symbol.iterator]();
    }
}

module.exports = LazySet;

🙁 Actual behavior

webpack/webpack#16957 (comment)

Argument of type 'LazySet<string>' is not assignable to parameter of type 'Iterable<string> | LazySet<string>'.
  Property '[Symbol.iterator]' is missing in type 'import("/home/jabaile/work/webpack-repro/lib/LazySet")<string>' but required in type 'import("/home/jabaile/work/webpack-repro/lib/LazySet")<string>'.ts(2345)
LazySet.js(27, 2): '[Symbol.iterator]' is declared here.

Note that LazySet is not assignable to LazySet, which is weird. Changing the signature of addAll to just be Iterable<T> is equivalent.

Also, if you actually construct this example in the editor, editing index.js will make the error go away temporarily, until tsserver is restarted, or LazySet.js is edited. Very odd.

🙂 Expected behavior

No errors.

@alexander-akait @TheLarkInn

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions