Skip to content

TS error when createServerFn returns a nested unknown type #4411

Closed
@rhyek

Description

@rhyek

Which project does this relate to?

Start

Describe the bug

The following:

const testUnknown = createServerFn().handler(() => {
  return {} as unknown as { a: unknown };
});

produces this error:

Argument of type '() => { a: unknown; }' is not assignable to parameter of type 'ServerFn<Method, "data", undefined, undefined, { a: unknown; }>'.
  Type '{ a: unknown; }' is not assignable to type 'Promise<{ a: {}; }> | { a: {}; }'.
    Type '{ a: unknown; }' is not assignable to type '{ a: {}; }'.
      Types of property 'a' are incompatible.
        Type 'unknown' is not assignable to type '{}'.ts(2345)

Your Example Website or App

https://stackblitz.com/edit/github-jwt7xpxn?file=src%2Froutes%2Findex.tsx

Steps to Reproduce the Bug or Issue

  1. Go to https://stackblitz.com/edit/github-jwt7xpxn?file=src%2Froutes%2Findex.tsx
  2. Go to index.tsx
  3. See TS error in server fn

Expected behavior

The return type of the server fn should include the never type and not {}.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • TS: 5.8.3
  • Tanstack Router: 1.121.2
  • Tanstack Start: 1.121.7

Additional context

My api endpoint is returning data that might include the unknown type and I want to be able to handle that in my components.

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