florian-lefebvre / astro-integration-kit

A package that contains utilities to help you build Astro integrations.
https://astro-integration-kit.netlify.app
MIT License
56 stars 8 forks source link

React typing required for type validation even when not used #78

Closed Fryuni closed 6 months ago

Fryuni commented 7 months ago

The barrel export as astro-integration-kit/utilities causes types for all utilities to be loaded, which requires all optional dependencies to be present, even if unused.

> @inox-tools/aik-route-config@0.1.0-alpha.2 validate /Users/lotus/IsoWorkspaces/OSS/inox-tools/packages/aik-route-config
> tsc

../../node_modules/.pnpm/astro-integration-kit@0.5.0_astro@4.4.4/node_modules/astro-integration-kit/src/utilities/add-devtoolbar-framework-app.ts:87:10 - error TS2307: Cannot find module '@vitejs/plugin-react' or its corresponding type declarations.

87   import("@vitejs/plugin-react").then((react) => {
            ~~~~~~~~~~~~~~~~~~~~~~

Found 1 error in ../../node_modules/.pnpm/astro-integration-kit@0.5.0_astro@4.4.4/node_modules/astro-integration-kit/src/utilities/add-devtoolbar-framework-app.ts:87

 ELIFECYCLE  Command failed with exit code 2.

The solution for this is to include a module declaration in the file to prevent the module typing from depending on the presence of the dependency.

Since modules are merged like interfaces, the module declaration can declare a simple export without details.

florian-lefebvre commented 6 months ago

How would such thing look? That part of TS keeps confusing me ngl!

Fryuni commented 6 months ago
declare module "@vitejs/plugin-react" {
  const plugin: TheNeededType;
  export = plugin;
}

I have to check what is that type for that plugin, but I think it will be a normal Vite plugin.

This way it type-checks even when the lib is not installed.

florian-lefebvre commented 6 months ago

Gotcha I see