Skip to content

createServerFn handler encounters a type error when the return type contains a property of type unknown #5229

@axuj

Description

@axuj

Which project does this relate to?

Start

Describe the bug

When using createServerFn from @tanstack/start, a TypeScript error occurs if the handler's return type is an object that includes a property with the unknown type. The type inference seems to fail, causing a type mismatch error.

Your Example Website or App

https://tanstack.com/start/latest/docs/framework/react/server-functions#validation-with-zod

Steps to Reproduce the Bug or Issue

  1. Define a type with a property of type unknown.
  2. Create a server function using createServerFn.
  3. In the .handler(), return an object that conforms to the defined type.

example code:

import { createServerFn } from '@tanstack/start';

type Res = {
  text: string;
  metaData: unknown;
};

const loader = createServerFn().handler(async () => {
  const res: Res = {
    text: 'hello',
    metaData: {
      tag: 'first',
    },
  };
  return res;
});

error message

Argument of type '() => Promise<Res>' is not assignable to parameter of type 'ServerFn<Register, Method, undefined, undefined, Res>'.
  Type 'Promise<Res>' is not assignable to type 'ServerFnReturnType<Register, Res>'.
    Type 'Promise<Res>' is not assignable to type 'Promise<{ text: string; metaData: {}; }>'.
      Type 'Res' is not assignable to type '{ text: string; metaData: {}; }'.
        Types of property 'metaData' are incompatible.
          Type 'unknown' is not assignable to type '{}'.ts(2345)

Expected behavior

Correct use of type

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.132.6
  • OS: Windows
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Browser Version: [e.g. 91.1]
  • Bundler: vite
  • Bundler Version: 7.1.7

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions