Skip to content

Issue with TypeScript definitions that use Enum members #54

@vladatnyc

Description

@vladatnyc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions