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

Improve emitted type declarations for TS 5.5+ #116

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading