Skip to content
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

Is there a way to define how types are written? #599

Closed
chris-acuvity opened this issue Jun 7, 2024 · 1 comment
Closed

Is there a way to define how types are written? #599

chris-acuvity opened this issue Jun 7, 2024 · 1 comment
Labels

Comments

@chris-acuvity
Copy link

Currently json-schema-to-typescript generates the following code:

export type Operator =
  | "Equals"
  | "NotEquals"
  | "Contains"
  | "NotContains"
  | "EqualsOrGreaterThan"
  | "EqualsOrLesserThan"
  | "OneOf"
  | "Empty"
  | "NotEmpty"; 

// same for keys
// same for values

export interface Predicate {
  key: Key;
  operator: Operator;
  values: Values;
}

It is valid and working fine, however, I could not find a way to enumerate all the possible values of an operator for other operation. For instance, I would want to create a Record<Operator, string> to translate the Operator.

I want to avoid writing the following to make sure when my schema changes, the dictionnary changes too:

const translation = {
 "Equals": "is equal to",
 "NotEquals": "does not equal",
  // and everything else is also done manually...
}

The problem above is that I need to re-instanciate a new string for each operator.

Is there a way to do this in TypeScript or a way to change the json-schema-to-typescript output ?

I have been looking at this article which suggest to have something like:

export const OperatorValues = ['Equals', 'NotEquals'] as const; 
type OperatorType = typeof OperatorValues[number]; 

Thanks for your input 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants