Skip to content

Weird typings in server functions #2950

@arackaf

Description

@arackaf

Which project does this relate to?

Start

Describe the bug

Type system does not seem to live primitives being returned from validator.

Code is below. Returning the literal 12 from validator results in ctx.data being any. as const does not change that. Interestingly, as number does get the typing correct.

import { createFileRoute, Link } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/start";

export const serverFn1 = createServerFn({ method: "GET" })
  .validator((x: string) => {
    return 12;
  })
  .handler((ctx) => {
    const x = ctx.data; // x is any
    return { time: "" };
  });

export const serverFn2 = createServerFn({ method: "GET" })
  .validator((x: string) => {
    return 12 as const;
  })
  .handler((ctx) => {
    const x = ctx.data; // x is any
    return { time: "" };
  });

export const Route = createFileRoute("/")({
  component: Home,
  async loader() {
    return {};
  },
});

function Home() {
  return (
    <div>
      <Link to="/foo">Foo</Link>
      <div>Hello</div>
    </div>
  );
}

Your Example Website or App

https://github.com/arackaf/tanstack-start-test-1/blob/special/typings-weird-issue/app/routes/index.tsx

Steps to Reproduce the Bug or Issue

Clone and install from the repo above, look at the file linked above, lines 9 and 18

Expected behavior

x should be number

Screenshots or Videos

No response

Platform

n/a

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    startEverything about TanStack StarttypesChanges to the typescript types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions