Skip to content

Commit

Permalink
Add Props generic to SSROptions (alephjs#402)
Browse files Browse the repository at this point in the history
This change adds a generic type parameter to the `SSROptions` type that,
as a result, provides a type for the returned props object. This models
after React's type generics for component types.
  • Loading branch information
tatemz authored Sep 22, 2021
1 parent d4efff8 commit bd32a27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 9 additions & 3 deletions examples/hello-world-ssr/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React from 'https://esm.sh/react'
import React, { FC } from 'https://esm.sh/react'
import type { SSROptions } from 'https://deno.land/x/aleph/types.d.ts'

export const ssr: SSROptions = {
type Props = {
serverTime: number
}

export const ssr: SSROptions<Props> = {
props: async router => {
return {
$revalidate: 1, // revalidate props after 1 second
Expand All @@ -13,8 +17,10 @@ export const ssr: SSROptions = {
}
}

export default function Page(props) {
const Page: FC<Props> = (props) => {
return (
<p>Now: {props.serverTime}</p>
)
}

export default Page
7 changes: 3 additions & 4 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,15 @@ export type GlobalSSROptions = {
/**
* The **SSR** props.
*/
export type SSRProps = {
[key: string]: any
export type SSRProps<Props> = Props & {
$revalidate?: number
}

/**
* The **SSR** options for pages.
*/
export type SSROptions = {
props?(router: RouterURL): (SSRProps | Promise<SSRProps>)
export type SSROptions<Props = {}> = {
props?(router: RouterURL): (SSRProps<Props> | Promise<SSRProps<Props>>)
paths?(): (string[] | Promise<string[]>)
}

Expand Down

0 comments on commit bd32a27

Please sign in to comment.