@kubb/plugin-ts 5.0.0-beta.75 → 5.0.0-beta.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -792,24 +792,30 @@ function buildPropertyType(schema, baseType, optionalType, optional) {
792
792
  if ((optional || meta.nullish || meta.optional) && addsUndefined) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
793
793
  return type;
794
794
  }
795
+ const indexSignaturePrinter = typescript.default.createPrinter();
796
+ const indexSignatureSource = typescript.default.createSourceFile("", "", typescript.default.ScriptTarget.Latest);
795
797
  /**
796
- * Creates TypeScript index signatures for `additionalProperties` and `patternProperties` on an object schema node.
798
+ * Creates a TypeScript index signature for `additionalProperties` and `patternProperties` on an
799
+ * object schema node. TypeScript allows only one string index signature and its value must be
800
+ * assignable from every named property, so both keywords collapse into a single signature: the
801
+ * union of all value types (deduplicated), or `unknown` when the object also has fixed properties.
802
+ * The key regex of `patternProperties` cannot be expressed by an index signature and is dropped.
797
803
  */
798
804
  function buildIndexSignatures(node, propertyCount, print) {
799
- const elements = [];
800
- if (node.additionalProperties && node.additionalProperties !== true) {
801
- const additionalType = print(node.additionalProperties) ?? keywordTypeNodes.unknown;
802
- elements.push(createIndexSignature(propertyCount > 0 ? keywordTypeNodes.unknown : additionalType));
803
- } else if (node.additionalProperties === true) elements.push(createIndexSignature(keywordTypeNodes.unknown));
804
- if (node.patternProperties) {
805
- const first = Object.values(node.patternProperties)[0];
806
- if (first) {
807
- let patternType = print(first) ?? keywordTypeNodes.unknown;
808
- if (first.nullable) patternType = createUnionDeclaration({ nodes: [patternType, keywordTypeNodes.null] });
809
- elements.push(createIndexSignature(patternType));
810
- }
805
+ const valueTypes = [];
806
+ if (node.additionalProperties && node.additionalProperties !== true) valueTypes.push(print(node.additionalProperties) ?? keywordTypeNodes.unknown);
807
+ else if (node.additionalProperties === true) valueTypes.push(keywordTypeNodes.unknown);
808
+ for (const schema of Object.values(node.patternProperties ?? {})) {
809
+ const patternType = print(schema) ?? keywordTypeNodes.unknown;
810
+ valueTypes.push(schema.nullable ? createUnionDeclaration({ nodes: [patternType, keywordTypeNodes.null] }) : patternType);
811
811
  }
812
- return elements;
812
+ if (valueTypes.length === 0) return [];
813
+ const seen = /* @__PURE__ */ new Set();
814
+ const distinct = valueTypes.filter((type) => {
815
+ const key = indexSignaturePrinter.printNode(typescript.default.EmitHint.Unspecified, type, indexSignatureSource);
816
+ return seen.has(key) ? false : (seen.add(key), true);
817
+ });
818
+ return [createIndexSignature(propertyCount > 0 ? keywordTypeNodes.unknown : createUnionDeclaration({ nodes: distinct }))];
813
819
  }
814
820
  //#endregion
815
821
  //#region src/components/Enum.tsx