-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Make it possible to type context and props for LoadInput and LoadOutput #1447
Conversation
- added generics for Context and Props - added a helper type "MaybePromise" to not write T | Promise<T> all the time Fixes #1442
Since only <script context="module">
import { Load } from '@sveltejs/kit';
import { InputContextType, OutputContextType, PropsType } from './somewhere'
export const load: Load<InputContextType, OutputContextType, PropsType> = async ({ context }) => {
// ...
return {
props: {},
context: {}
}
}
</script> |
Might be slightly off-topic: Are there any thoughts on using |
Using |
Is this sort of necessary as a pre-requisite to #647? As an end-user, I think what's described there is less verbose. We should think about how these efforts would interact. If the user is typing out the types we might come to a different conclusion on named versus positional arguments than if they're autogenerated |
It's a prerequisite, yes, and it means we need even more generics for input, namely one for |
Yes, that is in essence what I was eluding to. Not sure why @benmccann closed my issue so didn't initially see this reply/tag... |
Added type inference helper to get correct type in case generic is not set
I changed this to using two generic parameters - one for the input, one for the output - which each have named arguments. I think this feels more natural than having one big generic for both inputs and outputs. It also avoids the "how to name input/output context" question. I also added the Once #984 is decided we have to rename the named arguments introduced in here. |
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.
👍 Looks good! I like the type helpers.
I think it's also a good idea to add type tests, especially early on to catch type changes more easily. I suggest, maybe we use |
I think the test code is type checked since #1319 so if we just add types to a couple of the existing tests that should cover it |
On second thought, can we wait a bit for this. Quoting from a certain moderator in the TypeScript server
I was also wondering why IntelliSense didn't pick up correctly inside the |
As for the type tests using |
On |
After multiple shower thoughts, I think it's best if we export |
@dummdidumm it looks like this one will need to be rebased
I'm not sure I understand why that wouldn't help. If we have Svelte files with |
The current tests are testing the type correctness in a very rough way. I'd like to be more granular about it, and adding e2e tests for that purpose is wrong. Also I can't test that I expect a specific kind of error. That's why there's value in specific type tests. |
Can we also add |
Excuse me if this is a dumb question: Aren't the current |
There's |
My bad, I missed that. A bare |
Just checked, this was already done in #1617 , you are allowed to do |
@dummdidumm Could you show a usage example? |
Fixes #1442
@puchesjr is this what you had in mind? Usage would look like this:
We could bikeshed if the output context generic should appear before the props generic.