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: pageprops type improvements with docs for use #1001

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/create-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ Once this is done, any `<Link />` component or hook from `waku/router` that uses

Note: The file-based router paths will be supported in the future with some form of code-generation to get the types from your local page files.

#### PageProps

The above snippet will also provide types for the `PageProps` type. This will give type safety for the slugs in your pages.

```tsx
// ./src/pages/about.tsx
import type { PageProps } from 'waku/router';

// PageProps<'/about/[foo]'> => { path: `/about/${string}`; foo: string; query: string; }
export default function AboutPage({ path }: PageProps<'/about/[foo]'>) {
return <>{/* ...*/}</>;
}
```

### Pages

#### Single routes
Expand Down
2 changes: 1 addition & 1 deletion examples/03_demo/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ const shuffle = (array: Array<any>) => {
/**
* Mock static paths
*/
export const getPokemonPaths = async () => {
export const getPokemonPaths = async (): Promise<string[]> => {
return pokemon.map((row: any) => row.slug);
};
5 changes: 2 additions & 3 deletions examples/03_demo/src/pages/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Link } from 'waku';
import type { PageProps } from 'waku/router';

import { getPokemonPaths } from '../lib';
import { pokemon } from '../lib/pokemon';

type PokemonPageProps = { slug: string };

export default async function PokemonPage({ slug }: PokemonPageProps) {
export default async function PokemonPage({ slug }: PageProps<'/[slug]'>) {
const pokemon = await getPokemon(slug);

if (!pokemon) {
Expand Down
4 changes: 3 additions & 1 deletion examples/11_fs-router/src/pages/nested/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Page = ({ name }: { name: string }) => (
import type { PageProps } from 'waku/router';

const Page = ({ name }: PageProps<'/nested/[name]'>) => (
<div>
<h2>Nested / {name}</h2>
</div>
Expand Down
5 changes: 3 additions & 2 deletions packages/waku/src/router/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export interface CreatePagesConfig {
}

/** Props for pages when using `createPages` */
export type PageProps<Path extends PagePath<CreatePagesConfig>> =
PropsForPages<Path>;
export type PageProps<
Path extends PagePath<CreatePagesConfig> | (string & {}),
> = PropsForPages<Path>;
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ export type GetSlugs<Route extends string> = _GetSlugs<Route>;

/** Paths with slugs as string literals */
export type PagePath<Config> = Config extends {
pages: infer AllPages;
pages: { DO_NOT_USE_pages: { path: infer Path } };
}
? AllPages
? Path
: never;

type IndividualSlugType<Slug extends string> = Slug extends `...${string}`
Expand Down
Loading