lbovet / typson

Converts TypeScript to JSON-schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to generate string enums

zengfenfei opened this issue · comments

I'm using react-jsonschema-form to generate UI with the schema generated by typson.

Current generated schema for:

export enum CallDirection {
	Inbound,
	Outbound
}

is :

{
	"id": "CallDirection",
	"enum": [
		"Inbound",
		"Outbound"
	]
}

There is no type property in the generated schema. But react-jsonschema-form requires a type property to render the UI.

So I try to define the string enums like this:

type CallDirection = "Inbound" | "Outbound";

But typson will report error:

lib/Config.ts(10,1): error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.
lib/Config.ts(10,13): error TS1005: ';' expected.

/Users/kevin.zeng/.config/yarn/global/node_modules/q/q.js:126
                    throw e;

I also try to add the type property by annotation, but it doesnot work:

/**
 * @type string
 */
export enum CallDirection {
	Inbound,
	Outbound
}

Is there any suggestions for me to generate string enums with default values, like:

{
	"id": "CallDirection",
	"type": "string",
	"enum": [
		"Inbound",
		"Outbound"
	],
	"default": "Outbound"
}

Thanks in advance.