Skip to content

Commit

Permalink
Improve emitted type declarations for TS 5.5+ (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni authored Jul 22, 2024
1 parent 5fa1688 commit 8d5a476
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 169 deletions.
127 changes: 127 additions & 0 deletions .changeset/flat-bugs-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
"astro-integration-kit": minor
---

Improve emitted inferred types for libraries

For the following code:

```ts
export const utility = defineUtility('astro:config:setup')((
params: HookParameters<'astro:config:setup'>,
options: { name: string }
) => {
// do something
});

export const integration defineIntegration({
name: 'some-integration',
setup: () => ({
hooks: {
'astro:config:setup': (params) => {
// do something
},
},
something: (it: string): string => it,
}),
});
```

Previously, the emitted declarations would be:

```ts
import * as astro from "astro";
import { AstroIntegrationLogger } from "astro";

type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? DeepPartial<U>[]
: T[P] extends object | undefined
? DeepPartial<T[P]>
: T[P];
};

export const utility: (
params: {
config: astro.AstroConfig;
command: "dev" | "build" | "preview";
isRestart: boolean;
updateConfig: (
newConfig: DeepPartial<astro.AstroConfig>
) => astro.AstroConfig;
addRenderer: (renderer: astro.AstroRenderer) => void;
addWatchFile: (path: URL | string) => void;
injectScript: (stage: astro.InjectedScriptStage, content: string) => void;
injectRoute: (injectRoute: astro.InjectedRoute) => void;
addClientDirective: (directive: astro.ClientDirectiveConfig) => void;
addDevOverlayPlugin: (entrypoint: string) => void;
addDevToolbarApp: (entrypoint: astro.DevToolbarAppEntry | string) => void;
addMiddleware: (mid: astro.AstroIntegrationMiddleware) => void;
logger: AstroIntegrationLogger;
},
options: {
name: string;
}
) => void;
export const integration: () => astro.AstroIntegration &
Prettify<
Omit<
ReturnType<
() => {
hooks: {
"astro:config:setup": (params: {
config: astro.AstroConfig;
command: "dev" | "build" | "preview";
isRestart: boolean;
updateConfig: (
newConfig: DeepPartial<astro.AstroConfig>
) => astro.AstroConfig;
addRenderer: (renderer: astro.AstroRenderer) => void;
addWatchFile: (path: URL | string) => void;
injectScript: (
stage: astro.InjectedScriptStage,
content: string
) => void;
injectRoute: (injectRoute: astro.InjectedRoute) => void;
addClientDirective: (
directive: astro.ClientDirectiveConfig
) => void;
addDevOverlayPlugin: (entrypoint: string) => void;
addDevToolbarApp: (
entrypoint: astro.DevToolbarAppEntry | string
) => void;
addMiddleware: (mid: astro.AstroIntegrationMiddleware) => void;
logger: AstroIntegrationLogger;
}) => void;
};
something: (it: string) => string;
}
>,
keyof astro.AstroIntegration
>
>;
```

Now the emitted declarations would be:

```ts
import * as astro from "astro";
import * as astro_integration_kit from "astro-integration-kit";

export const utility: astro_integration_kit.HookUtility<
"astro:config:setup",
[
options: {
name: string;
}
],
void
>;
export const integration: () => astro.AstroIntegration & {
something: (it: string) => string;
};
```
Loading

0 comments on commit 8d5a476

Please sign in to comment.