Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDoc type annotations on module.exports assignments not type checked #47107

Open
5 tasks done
jpage-godaddy opened this issue Dec 10, 2021 · 2 comments
Open
5 tasks done
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@jpage-godaddy
Copy link

jpage-godaddy commented Dec 10, 2021

🔍 Search Terms

jsdoc module.exports 

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Adding a @type JSDoc annotation to module.exports should type check like it does in other contexts.

📃 Motivating Example

Currently, given the following blah/some-type.ts:

export default interface SomeType {
  name: string,
  age: number
}

...this doesn't type check:

/** @type {import('./blah/some-type').default} */
module.exports = {
  name: 0,
  age: 'forty-three'
};

...but this does, with some extra steps:

/** @type {import('./blah/some-type').default} */
const obj = {
  name: 0,
  age: 'forty-three'
};

module.exports = obj;

This is similar to #27327, but a simple thing like adding a @type {string} to module.exports does work. An imported type does not.

💻 Use Cases

We have a CommonJS JS config file that we'd like to enable users to type check. The expected way you'd think you'd add a type annotation to it doesn't work.

@RyanCavanaugh RyanCavanaugh added Domain: JSDoc Relates to JSDoc parsing and type generation Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Dec 14, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 14, 2021
@aaronadamsCA
Copy link

aaronadamsCA commented Mar 25, 2023

I've run into the same problem with different details.

On TypeScript 4.9.5, typing module.exports with an imported type is fine - as long as the file does not include JSX syntax.

This gatsby-browser.jsx type checks:

/** @type {import("gatsby").GatsbyBrowser} */
module.exports = {
  shouldUpdateScroll: ({ routerProps }) =>
    routerProps.location.state?.shouldUpdateScroll ?? true,
};

This one does not:

const { RouteUpdateProgress } = require("./src/components/RouteUpdateProgress");

/** @type {import("gatsby").GatsbyBrowser} */
module.exports = {
  shouldUpdateScroll: ({ routerProps }) => // Binding element 'routerProps' implicitly has an 'any' type.
    routerProps.location.state?.shouldUpdateScroll ?? true,
  wrapPageElement: ({ element }) => ( // Binding element 'element' implicitly has an 'any' type.
    <RouteUpdateProgress>{element}</RouteUpdateProgress>
  ),
};

Workaround is same as OP.

@karlhorky
Copy link
Contributor

Ah looks like this issue is also similar to (or maybe even the same as) my issue over here:

36degrees added a commit to alphagov/govuk-design-system that referenced this issue Apr 11, 2024
There’s an issue with the TypeScript compiler that prevents direct assignments to `module.exports` being type checked. [1]

We can work around this by assigning the config to a const first, then exporting it as a second step.

This means that the invalid config is correctly flagged when running `npm run lint:types` and in VS Code.

[1]: microsoft/TypeScript#47107
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
- this adds a simple set of types to the @tryghost/api-framework
  package that should describe all of the keys available on a
  controller, and then rolls it out to all API controllers
- unfortunately, due to microsoft/TypeScript#47107, we have
  to split apart `module.exports` into a variable assignment in order for type-checking
  to be done
- the main benefit of this is that `frame` is now typed, and editors understand what keys
  are available, so intellisense works properly
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
- this adds a simple set of types to the @tryghost/api-framework
  package that should describe all of the keys available on a
  controller, and then rolls it out to all API controllers
- unfortunately, due to microsoft/TypeScript#47107, we have
  to split apart `module.exports` into a variable assignment in order for type-checking
  to be done
- the main benefit of this is that `frame` is now typed, and editors understand what keys
  are available, so intellisense works properly
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
- this adds a simple set of types to the @tryghost/api-framework
  package that should describe all of the keys available on a
  controller, and then rolls it out to all API controllers
- unfortunately, due to microsoft/TypeScript#47107, we have
  to split apart `module.exports` into a variable assignment in order for type-checking
  to be done
- the main benefit of this is that `frame` is now typed, and editors understand what keys
  are available, so intellisense works properly
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
- this adds a simple set of types to the @tryghost/api-framework
  package that should describe all of the keys available on a
  controller, and then rolls it out to all API controllers
- unfortunately, due to microsoft/TypeScript#47107, we have
  to split apart `module.exports` into a variable assignment in order for type-checking
  to be done
- the main benefit of this is that `frame` is now typed, and editors understand what keys
  are available, so intellisense works properly
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
- this adds a simple set of types to the @tryghost/api-framework
  package that should describe all of the keys available on a
  controller, and then rolls it out to all API controllers
- unfortunately, due to microsoft/TypeScript#47107, we have
  to split apart `module.exports` into a variable assignment in order for type-checking
  to be done
- the main benefit of this is that `frame` is now typed, and editors understand what keys
  are available, so intellisense works properly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants