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

Type RequiredOptions in "prettier" module is not augmented with plugin options #169

Closed
1 task done
jeremy-code opened this issue May 24, 2024 · 1 comment · Fixed by #172
Closed
1 task done
Assignees

Comments

@jeremy-code
Copy link
Contributor

jeremy-code commented May 24, 2024

Your Environment

  • Prettier version: 2.x.x
  • node version: v22.2.0
  • package manager: pnpm@9.1.2
  • IDE: VSCode (v1.89.1)

Describe the bug

TypeScript module augmentation with the "prettier" module is not supported, meaning using JSDoc types for the plugin options are not correctly denoted in IDEs (and presumably when the prettier config are typed-checked with TypeScript).

To Reproduce

  1. Open an IDE with TypeScript support.
  2. See what type is given for any of the plugin options (e.g. importOrder, importOrderTypeScriptVersion, importOrderParserPlugins).
  3. See it is based on what is inferred (usually never[] or string[]) and does not have the corresponding JSDoc comments.

Expected behavior

As shown in the second screenshot, I expect the correct types for the options to be given, but also when using the Config type from prettier (first screenshot). It's a minor quibble, since import("@ianvs/prettier-plugin-sort-imports").PrettierConfig is also provided, but that is a bit long.

I believe the code that would make this possible something like:

// @ianvs/prettier-plugin-sort-imports/types/index.d.ts
declare module 'prettier' {
  interface RequiredOptions extends PluginConfig {}
}

but when I attempted the trivial fix, I had some conflicts with the types in src/types.ts. I will take a look further, but I wanted to let you know beforehand.

Screenshots, code sample, etc

image

image

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

// .prettierrc.js
/** @type {import(prettier").Config} */
module.exports = {
  plugins: [require("@ianvs/prettier-plugin-sort-imports")],
  importOrder: [], // never[], but should be string[] | undefined
};

Error log

No errors.

Contribute to @ianvs/prettier-plugin-sort-imports

  • I'm willing to fix this bug 🥇
@IanVS
Copy link
Owner

IanVS commented May 24, 2024

Thanks for the issue. I would happily accept a PR that addresses this!

@IanVS IanVS closed this as completed in #172 Jun 7, 2024
IanVS pushed a commit that referenced this issue Jun 7, 2024
- Add TypeScript module augmentation for the `prettier` module in
`types/index.d.ts` to add the `PluginConfig` interface to the `Options`
interface to support proper typing of plugin options in IDEs and during
type-checking
- Closes #169
- Compiles and passes tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants