Skip to content
Source
Sign inSign up
TanStack/router

[Start] Server Fn Type instantiation is excessively deep and possibly infinite error #4533

Send to my agent
Closedalvarosevilla95(GitHub profile, opens in a new tab) opened this issue 2 comments

Which project does this relate to?

Start

Describe the bug

createServerFn type inference breaks down on certain recursive types, that should otherwise be valid in terms of serialization as a valid response.

type JSONValue = null | string | number | boolean | { [value: string]: JSONValue } | JSONValue[]

export const test1 = createServerFn().handler(() => {
  return [] as JSONValue[]
})

export const test2 = createServerFn().handler(() => {
  const a: JSONValue = []
  // or const a: JSONValue = {}
  return a
})

export const test3 = createServerFn().handler<JSONValue>(() => {
  return []
})

all produce the following type error:

1. Type instantiation is excessively deep and possibly infinite. [2589]

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

See above

Expected behavior

JSONValue would be a valid response type for a server function

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.121.34

Additional context

No response

  1. similar type issue:

    Argument of type '() => Promise<PaginatedDocs>' is not assignable to parameter of type 'ServerFn<"GET", "data", undefined, undefined, PaginatedDocs>'. Type 'Promise<PaginatedDocs>' is not assignable to type 'Promise<{ docs: { id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; ... 12 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; createdAt: stri...'. Type 'Promise<PaginatedDocs>' is not assignable to type 'Promise<{ docs: { id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; ... 12 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; createdAt: stri...'. Type 'PaginatedDocs' is not assignable to type '{ docs: { id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; ... 12 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; createdAt: string; }[];...'. Types of property 'docs' are incompatible. Type 'KnowledgeBase[]' is not assignable to type '{ id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; tenant: number | { ...; }; ... 11 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; crea...'. Type 'KnowledgeBase' is not assignable to type '{ id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; tenant: number | { ...; }; ... 11 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; crea...'. Types of property 'parent' are incompatible. Type 'number | KnowledgeBase | null | undefined' is not assignable to type 'number | { id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; ... 12 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; createdAt: string; } |...'. Type 'KnowledgeBase' is not assignable to type 'number | { id: number; title: string; description?: string | null | undefined; coverImage?: number | { id: number; uid?: string | null | undefined; originalFilename?: string | null | undefined; ... 12 more ...; focalY?: number | ... 1 more ... | undefined; } | null | undefined; ... 8 more ...; createdAt: string; } |...'.ts(2345)

    export type PaginatedDocs<T = any> = {
        docs: T[];
        hasNextPage: boolean;
        hasPrevPage: boolean;
        limit: number;
        nextPage?: null | number | undefined;
        page?: number;
        pagingCounter: number;
        prevPage?: null | number | undefined;
        totalDocs: number;
        totalPages: number;
    };
    
    export interface KnowledgeBase {
      id: number;
      title: string;
      description?: string | null;
      coverImage?: (number | null) | AgentAsset;
      knowledgeBaseId?: string | null;
      parent?: (number | null) | KnowledgeBase;
      documents?: {
        docs?: (number | DocumentsKnowledgeBase)[];
        hasNextPage?: boolean;
        totalDocs?: number;
      };
      videos?: {
        docs?: (number | VideosKnowledgeBase)[];
        hasNextPage?: boolean;
        totalDocs?: number;
      };
      members?: {
        docs?: (number | KnowledgeBasesMember)[];
        hasNextPage?: boolean;
        totalDocs?: number;
      };
      owner?: (number | null) | Member;
      tenant: number | Tenant;
      updatedAt: string;
      createdAt: string;
    }
  2. I'm facing the same issue. I ended up writing a mapper function that doesn't use the complex type (Kysely JSON value in my case). I don't understand how to overcome this issue. Would be amazing if this can be fixed or documented.

Sign in to join this conversation.