Microsoft/TypeScript

JSDoc: module.exports direct literal assignment not type checked

Open

#51.819 aperta il 8 dic 2022

Vedi su GitHub
 (2 commenti) (3 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Domain: JSDocExperience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

Bug Report

First of all, thanks for your continued work on TypeScript and the ecosystem 🙌

Using module.exports = <object literal> with a JSDoc-style annotation before it does not check the type of the assigned value.

Using module.exports = <object literal> is a common pattern in tooling in the JS/TS ecosystem.

🔎 Search Terms

module.exports, node, Node.js, CommonJS, JSDoc

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Modules

⏯ Playground Link

Playground link with relevant code

💻 Code

const m = {}

/** @type {{ a: 1 }} */
m.exports = {
    a: 2, // ✅ Error, as expected
};

/** @type {{ a: 1 }} */
module.exports = {
    a: 2, // 💥 No error, unexpected
}

This also happens with a non-nested object:

/** @type {{ a: 1 }} */
const exports = {
    a: 2, // ✅ Error, as expected
};

/** @type {{ a: 1 }} */
module.exports = {
    a: 2, // 💥 No error, unexpected
}

Playground

🙁 Actual behavior

module.exports object is not type checked with JSDoc, but a variable is.

🙂 Expected behavior

module.exports should also be type checked against the type

Related issues

Guida contributor