@kubb/plugin-ts 5.0.0-beta.74 → 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.js CHANGED
@@ -753,24 +753,30 @@ function buildPropertyType(schema, baseType, optionalType, optional) {
753
753
  if ((optional || meta.nullish || meta.optional) && addsUndefined) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
754
754
  return type;
755
755
  }
756
+ const indexSignaturePrinter = ts.createPrinter();
757
+ const indexSignatureSource = ts.createSourceFile("", "", ts.ScriptTarget.Latest);
756
758
  /**
757
- * Creates TypeScript index signatures for `additionalProperties` and `patternProperties` on an object schema node.
759
+ * Creates a TypeScript index signature for `additionalProperties` and `patternProperties` on an
760
+ * object schema node. TypeScript allows only one string index signature and its value must be
761
+ * assignable from every named property, so both keywords collapse into a single signature: the
762
+ * union of all value types (deduplicated), or `unknown` when the object also has fixed properties.
763
+ * The key regex of `patternProperties` cannot be expressed by an index signature and is dropped.
758
764
  */
759
765
  function buildIndexSignatures(node, propertyCount, print) {
760
- const elements = [];
761
- if (node.additionalProperties && node.additionalProperties !== true) {
762
- const additionalType = print(node.additionalProperties) ?? keywordTypeNodes.unknown;
763
- elements.push(createIndexSignature(propertyCount > 0 ? keywordTypeNodes.unknown : additionalType));
764
- } else if (node.additionalProperties === true) elements.push(createIndexSignature(keywordTypeNodes.unknown));
765
- if (node.patternProperties) {
766
- const first = Object.values(node.patternProperties)[0];
767
- if (first) {
768
- let patternType = print(first) ?? keywordTypeNodes.unknown;
769
- if (first.nullable) patternType = createUnionDeclaration({ nodes: [patternType, keywordTypeNodes.null] });
770
- elements.push(createIndexSignature(patternType));
771
- }
766
+ const valueTypes = [];
767
+ if (node.additionalProperties && node.additionalProperties !== true) valueTypes.push(print(node.additionalProperties) ?? keywordTypeNodes.unknown);
768
+ else if (node.additionalProperties === true) valueTypes.push(keywordTypeNodes.unknown);
769
+ for (const schema of Object.values(node.patternProperties ?? {})) {
770
+ const patternType = print(schema) ?? keywordTypeNodes.unknown;
771
+ valueTypes.push(schema.nullable ? createUnionDeclaration({ nodes: [patternType, keywordTypeNodes.null] }) : patternType);
772
772
  }
773
- return elements;
773
+ if (valueTypes.length === 0) return [];
774
+ const seen = /* @__PURE__ */ new Set();
775
+ const distinct = valueTypes.filter((type) => {
776
+ const key = indexSignaturePrinter.printNode(ts.EmitHint.Unspecified, type, indexSignatureSource);
777
+ return seen.has(key) ? false : (seen.add(key), true);
778
+ });
779
+ return [createIndexSignature(propertyCount > 0 ? keywordTypeNodes.unknown : createUnionDeclaration({ nodes: distinct }))];
774
780
  }
775
781
  //#endregion
776
782
  //#region src/components/Enum.tsx