-
-
Notifications
You must be signed in to change notification settings - Fork 432
[TypeScript] Provide typings for preload function (especially this
context)
#1463
Comments
Ah, I forgot you could do that with I'd be happy for a PR implementing this! We added some types in #1402. The The One thing I kind of punted on with the initial type defs is figuring out how to share types between our internal code and what the user uses. I'm not sure whether that'd be hard or not as I hadn't gotten much time to spend on the problem. It's a little unusual because Sapper has a (and yes, you interpreted me correctly - there's a desire to allow the |
Maybe also export a function type so it could be easier to type <script context="module">
export const preload: SapperPreload = function preload () { }
</script> or a dummy helper function with a typed callback parameter but does nothing in the runtime function sapper_preload(preload: SapperPreload) {
return preload
} |
closes sveltejs#1463
How does one use the types defined in #1468? The documented method doesn't appear to work for a fresh |
This was fixed in #1598 but is not yet released. |
The problem is fixed after upgrading sapper to the latest version ( |
Is your feature request related to a problem? Please describe.
People are having a hard time trying to figure out how to type the preload function correctly, especially when using
this
(like inthis.fetch(...)
). It is possible to do by adding athis
type to thepreload
function, but this may not be widely known.Describe the solution you'd like
Provide typings for the
preload
function for people to import.Also note this in the official docs so it's easy to discover how to do this.
(I'm happy to help / provide a PR if you give me pointers on where to add this)
Describe alternatives you've considered
Don't provide the functions through some
this
context, but through a third parameter of the function:export async function preload(page, session, {fetch, error, redirect}) { ...}
. This would be a breaking change of course.Additional context
I also started thinking about adding something to the language-server but ditched that because you can type it yourself, and because the
this
-context could be enhanced by the user in the future (if I'm interpreting @benmccann correctly).The text was updated successfully, but these errors were encountered: