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

Type Context and Props #1442

Closed
puchesjr opened this issue May 13, 2021 · 5 comments · Fixed by #1447
Closed

Type Context and Props #1442

puchesjr opened this issue May 13, 2021 · 5 comments · Fixed by #1447

Comments

@puchesjr
Copy link

puchesjr commented May 13, 2021

Is your feature request related to a problem? Please describe.
The inability to define types for context and props is less than ideal for the development process.

Describe the solution you'd like
Ideally, would love to see the ability to define types for context and props.

Describe alternatives you've considered
We have considered and currently declare types during the variable declaration within the route.

How important is this feature to you?
This feature would help our development with potential bugs, autocomplete and type hints, and we think it would be beneficial for many throughout the Svelte ecosystem.

Additional Context
https://github.com/sveltejs/kit/blob/master/packages/kit/types/page.d.ts

export type LoadOutput = {
	status?: number;
	error?: string | Error;
	redirect?: string;
	props?: Record<string, any> | Promise<Record<string, any>>;
	context?: Record<string, any>;
	maxage?: number;
};
@Rich-Harris
Copy link
Member

In an ideal world, we'd be able to generate types for props automatically based on what's exported from the main script. i.e. if you have this...

<script>
  export let foo: string;
</script>

...then this would be typed automatically:

<script context="module">
  /** @type {import('$this'}.Load */
  export function load() {
    return {
      props: {
        foo: 42 // type error — number is not assignable to string
      }
    };
  }
</script>

See #647. No idea if this is possible

@benmccann
Copy link
Member

I'm going to close in favor of #647

@dummdidumm
Copy link
Member

I don't think these two are strictly related. This specific request can also be handled by expanding the LoadInput / LoadOutput interfaces with generics for these types. The user can then provide these if he wants better type checking.

dummdidumm pushed a commit that referenced this issue May 14, 2021
- added generics for Context and Props
- added a helper type "MaybePromise" to not write T | Promise<T> all the time

Fixes #1442
@puchesjr
Copy link
Author

I'm going to close in favor of #647

Tired of just seeing issues closed without trying to understand the full context.

@benmccann
Copy link
Member

We want to do this. But this is a subset of #647. Implementing #647 would require implementing this one as well and I didn't think it made sense to have two tickets to track it

There's also a PR open for this so even if we reopened it would be closed shortly when that PR is checked in so I'm not sure that it's really meaningful whether we reopen this or not

dummdidumm added a commit that referenced this issue May 31, 2021
…ut (#1447)

- added generics for Context, Page Params and Props
- added a helper type "MaybePromise" to not write T | Promise<T> all the time
- Make LoadInput/Output public

Fixes #1442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants