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

`strictIndexSignatures` option is ignored for single pattern properties

Open yorinasub17 opened this issue 2 years ago • 0 comments

When generating for objects that have patternProperties with the strictIndexSignatures option, json-schema-to-typescript does not honor the strictIndexSignatures option.

I traced this down to this area of code:

https://github.com/bcherny/json-schema-to-typescript/blob/c82696300269e6de73818372b07d13a2f9ceeba7/src/parser.ts#L407-L427

Where it returns the keyName as [k: string] in the singlePatternProperty case but doesn't update the keyName property of the ast, which is used by the strictIndexSignatures routine:

https://github.com/bcherny/json-schema-to-typescript/blob/c82696300269e6de73818372b07d13a2f9ceeba7/src/generator.ts#L156-L158

I attempted a fix in this PR, here: https://github.com/bcherny/json-schema-to-typescript/pull/560

Example schema

{
  "type": "object",
  "properties": {
    "maybe": {
      "type": "string",
    },
    "pattern": {
      "type": "object",
      "properties": {
        "maybe": {
          "type": "string"
        }
      },
      "patternProperties": {
        "leaf|tree": {
          "type": "array"
        }
      }
    }
  }
}

Expected output

export interface Experiment {
  maybe?: string;
  pattern?: {
    maybe?: string;
    /**
     * This interface was referenced by `undefined`'s JSON-Schema definition
     * via the `patternProperty` "leaf|tree".
     */
    [k: string]: unknown[] | undefined;
  };
  [k: string]: unknown | undefined;
}

Actual output

export interface Experiment {
  maybe?: string;
  pattern?: {
    maybe?: string;
    /**
     * This interface was referenced by `undefined`'s JSON-Schema definition
     * via the `patternProperty` "leaf|tree".
     */
    [k: string]: unknown[];
  };
  [k: string]: unknown | undefined;
}

Related

  • https://github.com/bcherny/json-schema-to-typescript/issues/235
  • https://github.com/bcherny/json-schema-to-typescript/issues/477

yorinasub17 avatar Nov 18 '23 15:11 yorinasub17