Skip to content

Type inference lost with t.KeyOf().anyOf inside generic function #1133

Closed Answered by terrxo
terrxo asked this question in Q&A
Discussion options

You must be logged in to vote

Nvm this is a dumb thing, this should not be a Tuple, I misunderstood the definition of a Tuple. This should just be an array with uniqueItems: true as the options then just use a union derived from keyof.

import type { TObject } from '@sinclair/typebox';
import { t } from 'elysia';

interface ModelOptions {
  resourceId: string;

  list: TObject;
}

const getListParams = <const T extends TObject>(list: T) => {
  return t.Object(
    {
      query: t.Optional(t.String()),
      queryColumns: t.Optional(
        t.Array(t.KeyOf(list), {
          uniqueItems: true,
        }),
      ),
      orderBy: t.KeyOf(list),
      order: t.Union([t.Literal('asc'), t.Literal('desc')], { default: 'asc' }

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by terrxo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant