Microsoft/TypeScript

JSDoc: module.exports direct literal assignment not type checked

Open

#51,819 opened on Dec 8, 2022

View on GitHub
 (2 comments) (3 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
Domain: JSDocExperience EnhancementHelp WantedSuggestion

Description

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

Contributor guide