-
Notifications
You must be signed in to change notification settings - Fork 396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export enum name to allow for casting #354
Comments
This is similar to #337 except for the suggestion to not even have to rely on propertyNames |
You can do this with Input: {
type: 'object',
properties: {
fruit: {
description: 'type of fruit',
type: 'string',
enum: ['BANANA', 'KIWI', 'CHERRY']
},
fruit2: {
description: 'type of fruit',
id: 'Fruit',
type: 'string',
enum: ['BANANA', 'KIWI', 'CHERRY'],
tsEnumNames: ['BANANA', 'KIWI', 'CHERRY']
}
}
} Output: /* tslint:disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/
export interface Demo {
/**
* type of fruit
*/
fruit?: "BANANA" | "KIWI" | "CHERRY";
fruit2?: Fruit;
[k: string]: unknown;
}
/**
* type of fruit
*/
export enum Fruit {
BANANA = "BANANA",
KIWI = "KIWI",
CHERRY = "CHERRY"
} Adding a new option to generate string enums instead of unions (without needing |
I think it's a really important principle not to have to rely on custom properties in your input JSON schema to end up with an exported named enum in TypeScript that you can use for casting your strings when coding. |
The following JSON schema
is converted to
in the generated interface.
That looks good, but this means you can't assign a string (even when the string is "BANANA", "KIWI" or "CHERRY") to an object that adheres to the interface, unless you cast it. However, without the d.ts file exporting the enum type there's no way you can actually cast the string to the enum.
The problem can be solved when json-schema-to-typescript would export the enum as follows
because that would allow consumers of the interface to cast a string to the enum as follows
Am I missing something or would this be a much needed improvement?
The text was updated successfully, but these errors were encountered: