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

feat(types): add generic type for plugin api #5112

Merged
merged 1 commit into from Aug 30, 2023

Conversation

sxzz
Copy link
Contributor

@sxzz sxzz commented Aug 28, 2023

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

Add a generic type for Plugin.api

@vercel
Copy link

vercel bot commented Aug 28, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2023 4:50pm

@sxzz sxzz changed the title feat(types): add generic for plugin api feat(types): add generic type for plugin api Aug 28, 2023
@codecov
Copy link

codecov bot commented Aug 28, 2023

Codecov Report

Merging #5112 (5489fb2) into master (1e81f94) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #5112   +/-   ##
=======================================
  Coverage   98.96%   98.96%           
=======================================
  Files         226      226           
  Lines        8394     8394           
  Branches     2303     2303           
=======================================
  Hits         8307     8307           
  Misses         31       31           
  Partials       56       56           

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

@lukastaegert lukastaegert merged commit bb6bfa7 into rollup:master Aug 30, 2023
12 checks passed
@sxzz sxzz deleted the feat/generic-plugin-api branch August 30, 2023 05:53
@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.29.0. You can test it via npm install rollup.

@ArnaudBarre
Copy link

vitejs/plugin-react is overloading the type like this:

declare module 'vite' {
  export interface Plugin {
    api?: {
      /**
       * Manipulate the Babel options of `@vitejs/plugin-react`
       */
      reactBabel?: ReactBabelHook
    }
  }
}

But this now fails with this generic types:

node_modules/vite/dist/node/index.d.ts:2856:11 - error TS2430: Interface 'import("/Users/arnaud/git/vite-plugin-react/packages/plugin-react/node_modules/vite/dist/node/index").Plugin<A>' incorrectly extends interface 'import("/Users/arnaud/git/vite-plugin-react/packages/plugin-react/node_modules/rollup/dist/rollup").Plugin<A>'.
  Types of property 'api' are incompatible.
    Type '{ reactBabel?: ReactBabelHook | undefined; } | undefined' is not assignable to type 'A | undefined'.
      Type '{ reactBabel?: ReactBabelHook | undefined; }' is not assignable to type 'A'.
        'A' could be instantiated with an arbitrary type which could be unrelated to '{ reactBabel?: ReactBabelHook | undefined; }'.

2856 interface Plugin<A = any> extends rollup.Plugin<A> {

I changed to try to directly overload the rollup types with:

declare module 'rollup' {
  export interface Plugin<A = any> {
    api?: {
      /**
       * Manipulate the Babel options of `@vitejs/plugin-react`
       */
      reactBabel?: ReactBabelHook
    }
  }
}

but I now get:

src/index.ts:84:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'api' must be of type 'A | undefined', but here has type '{ reactBabel?: ReactBabelHook | undefined; } | undefined'.

Does anyone knows how I can get the same behavior as before with this new type? Is there any expected way to type this?

@sxzz
Copy link
Contributor Author

sxzz commented Nov 26, 2023

The react plugin itself should solve it. Please track issue vitejs/vite-plugin-react#258

@ArnaudBarre
Copy link

Yeah but I don't know how to solve it

@adi-code
Copy link

adi-code commented Dec 1, 2023

@ArnaudBarre What about using types like

interface ViteReactPluginApi {
      /**
       * Manipulate the Babel options of `@vitejs/plugin-react`
       */
      reactBabel?: ReactBabelHook;
}

type ViteReactPlugin = Plugin<ViteReactPluginApi>;

instead of declare?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants