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

Conversation

Fryuni
Copy link
Collaborator

@Fryuni Fryuni commented Jul 22, 2024

Some changes in TS 5.5 caused the declarations emitted from our inferred types to include internal types, both ours and from Astro. This causes

Example

Take this simplified example from Inox Tools as a base:

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

export default defineIntegration({
	name: '@inox-tools/runtime-logger',
	setup: () => ({
		hooks: {
			'astro:config:setup': (params) => {
				// do something
			},
		},
		something: (it: string): string => it,
	}),
});

With the new TS version, the following type declaration is emitted:

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];
};

declare const runtimeLogger: (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;
declare const _default: () => 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>>;

export { _default as default, runtimeLogger };

With the optimization the following declaration is emitted:

import * as astro from 'astro';
import * as astro_integration_kit from 'astro-integration-kit';

declare const runtimeLogger: astro_integration_kit.HookUtility<"astro:config:setup", [options: {
    name: string;
}], void>;
declare const _default: () => astro.AstroIntegration & {
    something: (it: string) => string;
};

export { _default as default, runtimeLogger };

Copy link

netlify bot commented Jul 22, 2024

Deploy Preview for astro-integration-kit ready!

Name Link
🔨 Latest commit 280f247
🔍 Latest deploy log https://app.netlify.com/sites/astro-integration-kit/deploys/669dc13e283b390008e50c9c
😎 Deploy Preview https://deploy-preview-116--astro-integration-kit.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Fryuni Fryuni force-pushed the feat/improve-emit-declarations branch from 7789e3f to 227f1dc Compare July 22, 2024 02:03
@Fryuni Fryuni marked this pull request as ready for review July 22, 2024 02:17
@Fryuni Fryuni self-assigned this Jul 22, 2024
@Fryuni Fryuni added the enhancement New feature or request label Jul 22, 2024
Copy link
Contributor

@Adammatthiesen Adammatthiesen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat. so much cleaner....

@@ -28,4 +28,6 @@ export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

export type ExtendedPrettify<T> = T extends infer U ? Prettify<U> : never;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a TS trick to force a generic to expand to its component types.

@florian-lefebvre florian-lefebvre merged commit 8d5a476 into main Jul 22, 2024
7 checks passed
@florian-lefebvre florian-lefebvre deleted the feat/improve-emit-declarations branch July 22, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants