Microsoft/TypeScript

JSDoc: module.exports direct literal assignment not type checked

Open

#51.819 aberto em 8 de dez. de 2022

Ver no GitHub
 (2 comments) (3 reactions) (0 assignees)TypeScript (6.726 forks)batch import
Domain: JSDocExperience EnhancementHelp WantedSuggestion

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

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

Guia do colaborador