florian-lefebvre / astro-integration-kit

A package that contains utilities to help you build Astro integrations.

Home Page:https://astro-integration-kit.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React typing required for type validation even when not used

Fryuni opened this issue · comments

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.

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

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.

Gotcha I see