Microsoft/TypeScript

JSDoc: module.exports direct literal assignment not type checked

Open

#51 819 ouverte le 8 déc. 2022

Voir sur GitHub
 (2 commentaires) (3 réactions) (0 assignés)TypeScript (6 726 forks)batch import
Domain: JSDocExperience EnhancementHelp WantedSuggestion

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

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

Guide contributeur