json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Override the key pattern for patternProperties ?

Open unilynx opened this issue 3 months ago • 0 comments

Would it be possible to add a setting to override TyeScript index key?

The following JSON schema

{
  "type": "object",
  "title": "test",
  "properties": {
    "gid": {
      "type": "string"
    }
  },
  "patternProperties": {
    "^x-.*$": {
      "type": "number"
    }
  }
}

Returns a type that makes the named property unusable

export interface Test {
  gid?: string;
  /**
   * This interface was referenced by `Test`'s JSON-Schema definition
   * via the `patternProperty` "^x-.*$".
   */
  [k: string]: number;
}

As gid will now be expected by TypeScript to be both a string and a number, even though my actual schema requires those properties to be prefixed with x- and they wouldn't conflict

I can work around this by setting tsType: unknown on the patterned property - at least TS will allow me to set gid to a string then.

If I could to something like tsKey: "[K in `x_${string}`]" I could work around this without the project having to attempt to infer a proper TS template. It would also offer a workaround for #518

unilynx avatar Sep 22 '25 09:09 unilynx