Microsoft/TypeScript

JSDoc: module.exports direct literal assignment not type checked

Open

#51,819 建立於 2022年12月8日

在 GitHub 查看
 (2 留言) (3 反應) (0 負責人)TypeScript (6,726 fork)batch import
Domain: JSDocExperience EnhancementHelp WantedSuggestion

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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

貢獻者指南