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

TS quirk for third-party plugins

Fryuni opened this issue · comments

If you do this:

export const somePlugin = definePlugin();

The inferred type of somePlugin requires importing transitive dependencies to be named (even if it is also your dependency) because it relies on AIK's dependency in Astro.
Because a third-party lib may depend on a different version of Astro, import('astro') would refer to different types depending on where it is. So TS rejects that just for the possibility, even if it is not really the case.

This problem happens for libs that are transpiled and emit a .d.ts. The two solutions are:

  • Set emitDeclarations and declarationsMap to false and publish the source TS files (like AIK does)
  • Explicitly create a named type for the plugin, like so:
    type SomePlugin = Plugin<
    	'utilityName',
    	'astro:config:setup',
    	(p: HookParams) => (params: UtilityParams) => UtilityOutput
    >;
    
    export const somePlugin: SomePlugin = definePlugin();

I better understand thanks! Well, I guess your first point is better DX wise but as long as there's a workaround, that's fine!