-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
differentiate between input and output params inside Load
#4160
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
differentiate between input and output params inside Load
#4160
Conversation
🦋 Changeset detectedLatest commit: 18d8baf The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
e000944 to
d2556d1
Compare
|
you'll need to run |
| export interface Load< | ||
| Params = Record<string, string>, | ||
| InputProps = Record<string, any>, | ||
| OutputProps = Record<string, any> | ||
| > { | ||
| (input: LoadInput<Params, InputProps>): MaybePromise< | ||
| Either<Fallthrough, LoadOutput<OutputProps>> | ||
| >; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good if we could omit OutputProps in the common case where they're just being passed through, I think?
| export interface Load< | |
| Params = Record<string, string>, | |
| InputProps = Record<string, any>, | |
| OutputProps = Record<string, any> | |
| > { | |
| (input: LoadInput<Params, InputProps>): MaybePromise< | |
| Either<Fallthrough, LoadOutput<OutputProps>> | |
| >; | |
| export interface Load< | |
| Params extends Record<string, string> = Record<string, string>, | |
| InputProps extends Record<string, any> = Record<string, any>, | |
| OutputProps extends Record<string, any> = InputProps | |
| > { | |
| (input: LoadInput<Params, InputProps>): MaybePromise< | |
| Either<Fallthrough, LoadOutput<OutputProps>> | |
| >; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I think this would make it a non-breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would still be a breaking change.
Before this PR props (InputProps) could hold any Record, If someone would have set the Props before (OutputProps), this would now become the InputProps so this could throw some errors.
Here is an example
Before:
<script lang="ts" context="module">
import type { Load } from '@sveltejs/kit'
type Props = { value: string }
export const load: Load<any, Props> = async ({ props }) => {
console.log(props.something) // valid
return { /* ... */ }
}
</script>After:
<script lang="ts" context="module">
import type { Load } from '@sveltejs/kit'
type Props = { value: string }
export const load: Load<any, Props> = async ({ props }) => {
console.log(props.something) // not valid anymore because only `value` is a known property
return { /* ... */ }
}
</script>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yep, gotcha. I've updated the changeset to reflect that. Breakingness aside, does this suggestion make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they make sense. I haven't spotted the missing extends in the generics because I was lazy and just copied the Props code from somewhere else ^^
Maybe it would make sense to take another look and update places where the extends is missing in the current code base? Should I take a look and include the fixes in this PR?
Also having the same InputProps and OutputProps could be a valid case 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's probably some other places that should use extends, but I'll merge this PR in the meantime anyway. Thank you!
Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpx changesetand following the prompts. All changesets should bepatchuntil SvelteKit 1.0I want to be able to type the Params of a
Loadfunction that get's it's props from an Endpoint.Currently the
InputParamsare always typed asRecord<string, any>Here is a simplified example:
/routes/[id].tsThis would be a breaking change