feat(packages/tuono): improve TuonoRouteProps type (#648)

This commit is contained in:
Marco Pasqualetti
2025-03-18 09:07:38 +01:00
committed by GitHub
parent 2301083edc
commit d562aa87a3
8 changed files with 59 additions and 15 deletions
+4 -4
View File
@@ -1,5 +1,5 @@
import type { JSX } from 'react'
import type { TuonoProps } from 'tuono'
import type { TuonoRouteProps } from 'tuono'
import { Link } from 'tuono'
interface IndexProps {
@@ -9,7 +9,7 @@ interface IndexProps {
export default function IndexPage({
data,
isLoading,
}: TuonoProps<IndexProps>): JSX.Element {
}: TuonoRouteProps<IndexProps>): JSX.Element {
if (isLoading) {
return <h1>Loading...</h1>
}
@@ -17,8 +17,8 @@ export default function IndexPage({
return (
<>
<h1>TUONO</h1>
<h2>{data?.subtitle}</h2>
<Link href={'/second-route'}>Routing link</Link>)
<h2>{data.subtitle}</h2>
<Link href={'/second-route'}>Routing link</Link>
</>
)
}
+2 -2
View File
@@ -1,5 +1,5 @@
import type { JSX } from 'react'
import type { TuonoProps } from 'tuono'
import type { TuonoRouteProps } from 'tuono'
interface IndexProps {
subtitle: string
@@ -8,7 +8,7 @@ interface IndexProps {
export default function IndexPage({
data,
isLoading,
}: TuonoProps<IndexProps>): JSX.Element {
}: TuonoRouteProps<IndexProps>): JSX.Element {
if (isLoading) {
return <h1>Loading...</h1>
}
+2 -2
View File
@@ -1,6 +1,6 @@
// src/routes/index.tsx
import type { JSX } from 'react'
import type { TuonoProps } from 'tuono'
import type { TuonoRouteProps } from 'tuono'
import PokemonLink from '../components/PokemonLink'
@@ -10,7 +10,7 @@ interface IndexProps {
export default function IndexPage({
data,
}: TuonoProps<IndexProps>): JSX.Element | null {
}: TuonoRouteProps<IndexProps>): JSX.Element | null {
if (!data?.results) return null
return (
@@ -1,6 +1,6 @@
// src/routes/pokemons/[pokemon].tsx
import type { JSX } from 'react'
import type { TuonoProps } from 'tuono'
import type { TuonoRouteProps } from 'tuono'
import { Link } from 'tuono'
import PokemonView from '../../components/PokemonView'
@@ -15,7 +15,7 @@ interface Pokemon {
export default function PokemonPage({
isLoading,
data,
}: TuonoProps<Pokemon>): JSX.Element {
}: TuonoRouteProps<Pokemon>): JSX.Element {
return (
<div>
<Link href="/">Back</Link>
+1 -1
View File
@@ -13,4 +13,4 @@ export {
export { TuonoScripts } from './shared/TuonoScripts'
export type { TuonoProps } from './types'
export type { TuonoRouteProps } from './types'
+33
View File
@@ -0,0 +1,33 @@
import { describe, it, expectTypeOf } from 'vitest'
import type { TuonoRouteProps } from './types'
describe('TuonoRouteProps', () => {
interface MyData {
something: string
}
type RouteProps = TuonoRouteProps<MyData>
it('should have correct union types', () => {
expectTypeOf<RouteProps>()
.toHaveProperty('isLoading')
.toEqualTypeOf<boolean>()
expectTypeOf<RouteProps>()
.toHaveProperty('data')
.toEqualTypeOf<null | MyData>()
})
it('should correctly infer `data` based upon `isLoading`', () => {
expectTypeOf<RouteProps>()
.extract<{ isLoading: true }>()
.toHaveProperty('data')
.toEqualTypeOf<null>()
expectTypeOf<RouteProps>()
.extract<{ isLoading: false }>()
.toHaveProperty('data')
.toEqualTypeOf<MyData>()
})
})
+9 -4
View File
@@ -47,7 +47,12 @@ export interface ServerPayload<TData = unknown> {
)
*/
export interface TuonoProps<T> {
data?: T
isLoading: boolean
}
export type TuonoRouteProps<TData> =
| {
data: null
isLoading: true
}
| {
data: TData
isLoading: false
}
+6
View File
@@ -12,6 +12,12 @@ export default mergeConfig(
*/
build: { target: 'es2022' },
plugins: [react()],
test: {
typecheck: {
enabled: true,
},
},
}),
defineViteConfig({
entry: [