bcherny/json-schema-to-typescript

Is there a way to define how types are written?

Closed this issue · 1 comments

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 🙏