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

feat(svelte2tsx): autotype const load = ... declarations #2540

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

GauBen
Copy link
Contributor

@GauBen GauBen commented Oct 21, 2024

Hi!

This PR adds autotyping to const load = ... declarations, opening the door to SvelteKit "plugins"

Usage might be:

export const load = myPlugin((pluginData, event) => {
  return pluginData
})

// Dummy plugin
// We write the generics this way for the types to flow from output to input
function myPlugin<T>(
  load: T extends (event: infer Event) => infer Output
    ? (data: Record<string, unknown>, event: Event) => Output
    : never,
): T {
  return ((event) => load({ hello: "World!" }, event)) as T;
}

It works by rewriting const load = ... as const load = (...) satisfies PageLoad, letting TypeScript do the generic instantiation.

satisfies was added in TS 4.9 and peer TS is set to ^4.9.4, so this should not be a breaking change

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

thank you!

@dummdidumm dummdidumm merged commit 77c9ccf into sveltejs:master Oct 31, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants