-
Notifications
You must be signed in to change notification settings - Fork 400
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
Named enum for string type #60
Comments
Hi @java2kus! TypeScript (like Java) does not support String => String enums. Enums in TS must be String => Number; see https://www.typescriptlang.org/docs/handbook/enums.html. You have a few options:
"status": {
"enum": [1, 2, 3],
"tsEnumNames": ["InProgress", "ForApproval", "Complete"]
}
=> export const enum Status {
InProgress = 1,
ForApproval = 2,
Complete = 3
}
export interface MyInterface {
status: Status
}
"status": {
"enum": ["InProgress", "ForApproval", "Complete"]
}
=> export interface MyInterface {
status: "InProgress" | "ForApproval" | "Complete"
} See all the valid ways to generate enums with json-schema-to-ts here. Hope that helps! |
@bcherny Thanks a lot for clarifying. Coming from a Java programming background, I am trying to wrap my head around Typescript :-). I guess I cannot solve my specific problem of substitution. But how I can achieve type safety on passing functions for the above. e.g. I am trying to create a function where one of the parameters is status. Currently I am defining this separately by adding a type like this which is leading to a lot of boilerplate and repetition in my code. In my case MyInterface has many more properties like taskDescription etc. If the generated interface above generates types for String literals my problem will be solved. Can you please let me know if this can be achieved in any way? "taskDescription": {
"type": "string"
}
"status": {
"enum": ["InProgress", "ForApproval", "Complete"]
}
==>
export type Status = "InProgress" | "ForApproval" | "Complete";
export interface MyInterface {
taskDescription: string;
status: Status;
}
.....
generateEvent(status: Status, taskDescription: string) { ... } |
@java2kus I'm not sure I understand what you're trying to do. The code you gave above will compile perfectly, is there something more you're trying to generate? |
@bcherny I was checking if the above code can be generated using json-schema-to-typescript library. Right now after the code generation, I am manually making all the above changes in my code. |
There is no support for named union types today. "taskDescription": {
"type": "string"
}
"status": {
"enum": ["InProgress", "ForApproval", "Complete"]
} Will generate export interface MyInterface {
taskDescription: string;
status: "InProgress" | "ForApproval" | "Complete";
} |
@bcherny This is how I have manually coded the string based enums in Typescript. I thought it would be useful for any future code generation enhancements. I am using Typescript 2.2. export enum Status {
InProgress = <any> 'I',
ForApproval = <any> 'A',
Complete = <any> 'C'
} |
@java2kus I don't think that actually works. It tricks the compiler into compiling the enum, but you don't get any type safety with it. What you want is microsoft/TypeScript#1206, or restructuring your code to use a number enum/union of string literal types. export enum Status {
InProgress = <any> 'I',
ForApproval = <any> 'A',
Complete = <any> 'C'
}
Status.A // Error
if (Status.InProgress === 'I') { } // Error |
@bcherny i too wanted to generate string enum from schema: https://www.typescriptlang.org/docs/handbook/enums.html#string-enums
Can you suggest some example for this? |
How do I generate named enums for string types. My JSON Schema looks like below:
Should generate the following Typescript
Instead of the above, its generating the flags without quotes that results in TypeScript error. Can you please suggest, how can I generate the above typescript code?
The text was updated successfully, but these errors were encountered: