-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Typing props
and stuff
in LoadOutput
and Load
#3392
Comments
props
in LoadOutput
props
and stuff
in LoadOutput
and Load
Update: After playing with this for hours, I just found out that the way you're supposed to type the properties of input and output of a export const load: Load<{ session: SessionType }, { props: PropsType }> = async () => {
return {
props: {
}
}
}; This is the first time I'm seeing this sort of thing but I understand the rationale behind it, it is brilliant, as it allows you to selectively choose the type of each of the properties, something that's not possible with regular TypeScript generic parameters. But at first glance it wasn't clear to me that you're supposed to do it like this at all; and I'm pretty sure I'm not alone in this. I asked a question about this in the Discord server and the answer I got from one of the moderators made the very same mistake that I made — here. So I'm going to keep this issue open as a suggestion to write about this in the docs. |
The interface You can see why it happens here: import type { InferValue, Rec } from "@sveltejs/kit/types/helper";
// the InferValue returns either type of the named property in the first type or the third type
type Test1 = InferValue<LoadOutput<Props>, 'props', Rec>; // type Test1 = { [x: string]: any; }
// the same but with just unfolded and simplified, for this case, `LoadOutput`.
type Test2 = InferValue<{props?: Props}, 'props', Rec>; // type Test2 = { [x: string]: any; }
type Test3 = InferValue<{props: Props}, 'props', Rec>; // type Test3 = { foo: number; } But |
@7nik Thanks for the explanation. I was confused about that specific point too, although I didn't mention it, now it makes sense. |
I doubt that the PR fixed the problem. As far as I can see, the types passed to MaybePromise<
Either<
Fallthrough,
LoadOutput<
InferValue<Required<Output>, 'props', Record<string, any>>,
InferValue<Required<Output>, 'stuff', Record<string, any>>
>
>
> It's should be okay to requirefy |
@ignatiusmb Sorry I don't understand how your PR resolved this issue?! |
What seems to be the problem here? The original issue points that you don't understand how to use the |
@ignatiusmb Sure but I was expecting an example (for the usage, if you will), as opposed to just the type definitions. |
We don't have any usage examples for using types in the docs, so it doesn't make sense to add one for this. |
Okay then I think it would be a good idea to add type usage examples to the docs in general, although I understand that that's beyond the scope of this issue. I don't really think type definitions in the docs are of much help, they're hard to read and parse and look like noise, honestly. |
I suppose not, but that shouldn't hopefully be an issue once we get to #3324 |
Describe the bug
I'm using TypeScript, and trying to specify the type of the
props
property in the return type of my load function. But I don't seem to be able to get it working:Also, when I do:
There seems to me to be something wrong with SvelteKit's
Load
type here.System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: