Skip to content

Commit

Permalink
fix: use partial record for enum keyed types (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Oct 19, 2023
1 parent 4c4d1bd commit d5eacb5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .changeset/thirty-turtles-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,19 @@ private void writeConstraintValidatorType(TypeScriptWriter writer, Shape shape)
writer.writeInline("Iterable<$T>", getSymbolForValidatedType(collectionMemberTargetShape));
} else if (shape.isMapShape()) {
MapShape mapShape = shape.asMapShape().get();
writer.writeInline("Record<$T, $T>",
getSymbolForValidatedType(mapShape.getKey()),
getSymbolForValidatedType(mapShape.getValue())
);
String keyType = getSymbolForValidatedType(mapShape.getKey()).toString();

if (keyType.equals("string")) {
writer.writeInline("Record<$T, $T>",
getSymbolForValidatedType(mapShape.getKey()),
getSymbolForValidatedType(mapShape.getValue())
);
} else {
writer.writeInline("Partial<Record<$T, $T>>",
getSymbolForValidatedType(mapShape.getKey()),
getSymbolForValidatedType(mapShape.getValue())
);
}
} else if (shape instanceof SimpleShape) {
writer.writeInline("$T", getSymbolForValidatedType(shape));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,17 @@ public Symbol setShape(SetShape shape) {
public Symbol mapShape(MapShape shape) {
Symbol key = toSymbol(shape.getKey());
Symbol value = toSymbol(shape.getValue());
return createSymbolBuilder(shape, format("Record<%s, %s>", key.getName(), value.getName()), null)
.addReference(key)
.addReference(value)
.build();

boolean stringKey = key.toString().equals("string");

return createSymbolBuilder(
shape,
format(stringKey ? "Record<%s, %s>" : "Partial<Record<%s, %s>>", key.getName(), value.getName()),
null
)
.addReference(key)
.addReference(value)
.build();
}

@Override
Expand Down

0 comments on commit d5eacb5

Please sign in to comment.