bcherny/json-schema-to-typescript

Support propertyNames with enum

Open

#337 opened on Sep 8, 2020

View on GitHub
 (1 comment) (2 reactions) (0 assignees)TypeScript (449 forks)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

propertyNames is included in the "not expressible in TS" list, but at least for the case with enum instead of pattern, this is not true. Consider the following example:

{
  "type": "object",
  "title": "workingHours",
  "definitions": {
    "weekdays": {"type": "string", "enum": ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]}
  },
  "additionalProperties": {"type": "number"},
  "propertyNames": {"$ref": "#/definitions/weekdays"}
}

could well be expressed in TS as

enum weekdays {
  monday = "monday",
  tuesday = "tuesday",
  wednesday = "wednesday",
  thursday = "thursday",
  friday = "friday",
  saturday = "saturday",
  sunday = "sunday"
}
type workingHours = {[k in weekdays]: number};

The example also shows what would be the purpose of this instead of specifying the properties literally: weekdays does not have to be repeated all the time when there are more types making use of its keys.

Contributor guide