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 { JSX } from 'react'
import type { TuonoProps } from 'tuono' import type { TuonoRouteProps } from 'tuono'
import { Link } from 'tuono' import { Link } from 'tuono'
interface IndexProps { interface IndexProps {
@@ -9,7 +9,7 @@ interface IndexProps {
export default function IndexPage({ export default function IndexPage({
data, data,
isLoading, isLoading,
}: TuonoProps<IndexProps>): JSX.Element { }: TuonoRouteProps<IndexProps>): JSX.Element {
if (isLoading) { if (isLoading) {
return <h1>Loading...</h1> return <h1>Loading...</h1>
} }
@@ -17,8 +17,8 @@ export default function IndexPage({
return ( return (
<> <>
<h1>TUONO</h1> <h1>TUONO</h1>
<h2>{data?.subtitle}</h2> <h2>{data.subtitle}</h2>
<Link href={'/second-route'}>Routing link</Link>) <Link href={'/second-route'}>Routing link</Link>
</> </>
) )
} }
+2 -2
View File
@@ -1,5 +1,5 @@
import type { JSX } from 'react' import type { JSX } from 'react'
import type { TuonoProps } from 'tuono' import type { TuonoRouteProps } from 'tuono'
interface IndexProps { interface IndexProps {
subtitle: string subtitle: string
@@ -8,7 +8,7 @@ interface IndexProps {
export default function IndexPage({ export default function IndexPage({
data, data,
isLoading, isLoading,
}: TuonoProps<IndexProps>): JSX.Element { }: TuonoRouteProps<IndexProps>): JSX.Element {
if (isLoading) { if (isLoading) {
return <h1>Loading...</h1> return <h1>Loading...</h1>
} }
+2 -2
View File
@@ -1,6 +1,6 @@
// src/routes/index.tsx // src/routes/index.tsx
import type { JSX } from 'react' import type { JSX } from 'react'
import type { TuonoProps } from 'tuono' import type { TuonoRouteProps } from 'tuono'
import PokemonLink from '../components/PokemonLink' import PokemonLink from '../components/PokemonLink'
@@ -10,7 +10,7 @@ interface IndexProps {
export default function IndexPage({ export default function IndexPage({
data, data,
}: TuonoProps<IndexProps>): JSX.Element | null { }: TuonoRouteProps<IndexProps>): JSX.Element | null {
if (!data?.results) return null if (!data?.results) return null
return ( return (
@@ -1,6 +1,6 @@
// src/routes/pokemons/[pokemon].tsx // src/routes/pokemons/[pokemon].tsx
import type { JSX } from 'react' import type { JSX } from 'react'
import type { TuonoProps } from 'tuono' import type { TuonoRouteProps } from 'tuono'
import { Link } from 'tuono' import { Link } from 'tuono'
import PokemonView from '../../components/PokemonView' import PokemonView from '../../components/PokemonView'
@@ -15,7 +15,7 @@ interface Pokemon {
export default function PokemonPage({ export default function PokemonPage({
isLoading, isLoading,
data, data,
}: TuonoProps<Pokemon>): JSX.Element { }: TuonoRouteProps<Pokemon>): JSX.Element {
return ( return (
<div> <div>
<Link href="/">Back</Link> <Link href="/">Back</Link>
+1 -1
View File
@@ -13,4 +13,4 @@ export {
export { TuonoScripts } from './shared/TuonoScripts' 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> { export type TuonoRouteProps<TData> =
data?: T | {
isLoading: boolean data: null
} isLoading: true
}
| {
data: TData
isLoading: false
}
+6
View File
@@ -12,6 +12,12 @@ export default mergeConfig(
*/ */
build: { target: 'es2022' }, build: { target: 'es2022' },
plugins: [react()], plugins: [react()],
test: {
typecheck: {
enabled: true,
},
},
}), }),
defineViteConfig({ defineViteConfig({
entry: [ entry: [