We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Start
serverOnly does not return the correct type.
As of v1.82.4, serverOnly is declared like this.
export declare function serverOnly<T>(value: () => T): () => T | undefined;
This means that when I have a serverOnly function like this:
import { serverOnly } from "@tanstack/start"; export const getPokemon = serverOnly(async (pokemon: string) => { return await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`).then(res => res.json()); });
getPokemon is inferred to have the type () => Promise<any> | undefined, which means passing the pokemon parameter leads to a typescript error
() => Promise<any> | undefined
pokemon
export const Route = createRootRoute({ beforeLoad: async () => { const pokemon = await getPokemon("ditto"); // ^ Expected 0 arguments, but got 1. (2554) return {pokemon} }, component: RootComponent, });
You can wrap the anonymous function inside another anonymous function to get the correct return type
import { serverOnly } from "@tanstack/start"; // Notice () => async () => {} export const getPokemon = serverOnly(() => async (pokemon: string) => { return await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`).then(res => res.json()); });
But you will have to "unwrap" once
export const Route = createRootRoute({ beforeLoad: async () => { const pokemon = await getPokemon?.()("ditto"); return { pokemon }; }, component: RootComponent, });
https://stackblitz.com/edit/github-avg9mj-9r1rad?file=app%2Froutes%2F__root.tsx
getPokemon should be inferred as (pokemon: string) => Promise<any> | undefined
(pokemon: string) => Promise<any> | undefined
No response
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Which project does this relate to?
Start
Describe the bug
serverOnly does not return the correct type.
As of v1.82.4, serverOnly is declared like this.
This means that when I have a serverOnly function like this:
getPokemon is inferred to have the type
() => Promise<any> | undefined
, which means passing thepokemon
parameter leads to a typescript errorAlternative method
You can wrap the anonymous function inside another anonymous function to get the correct return type
But you will have to "unwrap" once
Your Example Website or App
https://stackblitz.com/edit/github-avg9mj-9r1rad?file=app%2Froutes%2F__root.tsx
Steps to Reproduce the Bug or Issue
Expected behavior
getPokemon should be inferred as
(pokemon: string) => Promise<any> | undefined
Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: