IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
892 stars 21 forks source link

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

Closed jeremy-code closed 1 month ago

jeremy-code commented 1 month ago

Your Environment

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

IanVS commented 1 month ago

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