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

feat: add PageProps to $./types #12967

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

weebao
Copy link

@weebao weebao commented Nov 6, 2024

Closes #12726

Added a PageProps type for defining page props using the $props() rune.

<!--- file: page.svelte --->
<script>
	import { PageProps } from './$types';

	let { data }: PageProps = $props();
</script>

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Nov 6, 2024

🦋 Changeset detected

Latest commit: 4ad2dc8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Major

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

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-kit-12967-svelte.vercel.app/

this is an automated message

@eltigerchino
Copy link
Member

Did you accidentally forget to commit the changes? This PR only modifies the documentation

@dummdidumm dummdidumm added the needs-decision Not sure if we want to do this yet, also design work needed label Nov 11, 2024
@weebao
Copy link
Author

weebao commented Nov 12, 2024

I'm pretty sure I changed the code. I will check now. Thansk for the notice!

@eltigerchino
Copy link
Member

eltigerchino commented Nov 12, 2024

@weebao I think you meant to make the changes in the file below before regenerating the types.

/**
* Defines the common shape of the [$page.data store](https://svelte.dev/docs/kit/$app-stores#page) - that is, the data that is shared between all pages.
* The `Load` and `ServerLoad` functions in `./$types` will be narrowed accordingly.
* Use optional properties for data that is only present on specific pages. Do not add an index signature (`[key: string]: any`).
*/
export interface PageData {}
/**
* The shape of the `$page.state` object, which can be manipulated using the [`pushState`](https://svelte.dev/docs/kit/$app-navigation#pushState) and [`replaceState`](https://svelte.dev/docs/kit/$app-navigation#replaceState) functions from `$app/navigation`.
*/
export interface PageState {}

The types/index.d.ts file will get replaced based on the changes to this file.

@MathiasWP
Copy link
Contributor

Why have the "data" in the props? Wouldn't it be even cleaner to emit it?

@weebao
Copy link
Author

weebao commented Nov 13, 2024

Why have the "data" in the props? Wouldn't it be even cleaner to emit it?

That's because I want to not keep writing the code below on every page, so this is what I proposed.

<script>
	import { PageData } from './$types';

	interface PageProps {
		data: PageData;
	}

	let { data }: PageProps = $props();
</script>

Is this the correct way of doing it? This is my first time submitting a PR to this codebase so please let me know if I'm doing terribly wrong 😅

@MathiasWP
Copy link
Contributor

MathiasWP commented Nov 13, 2024

Why have the "data" in the props? Wouldn't it be even cleaner to emit it?

That's because I want to not keep writing the code below on every page, so this is what I proposed.

<script>
	import { PageData } from './$types';

	interface PageProps {
		data: PageData;
	}

	let { data }: PageProps = $props();
</script>

Is this the correct way of doing it? This is my first time submitting a PR to this codebase so please let me know if I'm doing terribly wrong 😅

What about something like this?

// +page.ts
export const load = () => {
	return {
		framework: "svelte",
		version: 5
	}
}
<script lang="ts">
// +page.svelte
import { PageProps } from './$types';

let { framework, version }: PageProps = $props()
</script>

A +page.svelte file cannot get its props from anything other than the load method, right? So why have the extra "data" variable wrapping? I see why it was needed with Svelte 4, but it feels unnecessary with Svelte 5.

@weebao
Copy link
Author

weebao commented Nov 13, 2024

@MathiasWP

Ohhh, I get your point. Having the data variable wrapper would make it redundant then. Right now I just made a simple fix for me and the other person who created the issue.

I think for your example case, the user would have to define their own CustomPageProps instead of importing from the global ./$types since I'm not sure how to implement PageProps to be customized for each page's data, but that would definitely make the code a lot cleaner! Can you show me how to implement this since I'm still not familiar with the codebase yet. Thank you!

@MathiasWP
Copy link
Contributor

MathiasWP commented Nov 13, 2024

@MathiasWP

Ohhh, I get your point. Having the data variable wrapper would make it redundant then. Right now I just made a simple fix for me and the other person who created the issue.

I think for your example case, the user would have to define their own CustomPageProps instead of importing from the global ./$types since I'm not sure how to implement PageProps to be customized for each page's data, but that would definitely make the code a lot cleaner! Can you show me how to implement this since I'm still not familiar with the codebase yet. Thank you!

Before spending time implementing it we should get an approval on the API from some of the svelte(kit) maintainers @Rich-Harris @dummdidumm @benmccann

@@ -7,6 +7,7 @@
* // interface Error {}
* // interface Locals {}
* // interface PageData {}
* // interface PageProps {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't belong here — PageProps isn't part of the App namespace, it's part of ./$types

Suggested change
* // interface PageProps {}

Comment on lines 44 to 50
/**
* Defines the reusable shape for [$props()](https://svelte.dev/docs/svelte/$props#Type-safety) for `+page.svelte` specifically
*/
export interface PageProps {
data: PageData;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
/**
* Defines the reusable shape for [$props()](https://svelte.dev/docs/svelte/$props#Type-safety) for `+page.svelte` specifically
*/
export interface PageProps {
data: PageData;
}

@Rich-Harris
Copy link
Member

Hi — thanks for the PR but I'm afraid this isn't the right way to do it — PageProps doesn't belong in the App namespace. The $types.d.ts file emitted for each route in write_types.js should contain something like this...

export type PageProps = { data: PageData, form: ActionData, children: Snippet };

(Note that it includes form, where appropriate, and children — in other words no, we do need a top-level data prop)

@weebao
Copy link
Author

weebao commented Nov 14, 2024

@Rich-Harris That actually makes so much sense. I'll work on it now

@@ -465,6 +465,12 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true

exports.push(`export type ${prefix}Data = ${data};`);

exports.push(
`export type ${prefix}Props = { data: ${prefix}Data; ${
node.server && is_page ? 'form: ActionData; ' : ''
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty dumb way to write this I know. I could've pushed this in the nested if above but I just thought writing it below PageData would be more intuitive. Also, didn't know how to add children in LayoutProps since I can't write import("svelte").Snippet as svelte isn't included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Not sure if we want to do this yet, also design work needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat : [Svelte5] Generate a PageProps into $types
5 participants