typebox-codegen icon indicating copy to clipboard operation
typebox-codegen copied to clipboard

Issue with TypeScript definitions that use Enum members

Open vladatnyc opened this issue 1 year ago • 0 comments

It seems that when a TypeScript type defines an element with reference to an Enum member, codegen does a straight copy of the definition which leads to an error. See exmaple below:

export enum Ops {
  Up = "up",
  Down = "down",
  Left = "left",
  Right = 'right'
}

export interface A {
  op: Ops.Down
}

is converted to:

import { Type, Static } from '@sinclair/typebox'

export enum EnumOps {
  Up = 'up',
  Down = 'down',
  Left = 'left',
  Right = 'right'
}

export type Ops = Static<typeof Ops>
export const Ops = Type.Enum(EnumOps)

export type A = Static<typeof A>
export const A = Type.Object({
  op: Ops.Down
})

This results in an error TypeError: Cannot read properties of undefined (reading '$id')

I believe that the correct output should be op: Type.Literal(EnumOps.Down) instead of op: Ops.Down. This resolves the error.

vladatnyc avatar Oct 24 '24 00:10 vladatnyc