Skip to content

Commit

Permalink
feat: allow undefined when property optional for exactOptionalPropert…
Browse files Browse the repository at this point in the history
…yTypes
  • Loading branch information
kuhe committed Nov 12, 2024
1 parent 0d9cee8 commit 3988115
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ void writeMembers(TypeScriptWriter writer, Shape shape) {
String optionalSuffix = shape.isUnionShape() || !isRequiredMember(member) ? "?" : "";
String typeSuffix = requiredMemberMode == RequiredMemberMode.NULLABLE
&& isRequiredMember(member) ? " | undefined" : "";
if (optionalSuffix.equals("?")) {
typeSuffix = " | undefined"; // support exactOptionalPropertyTypes.
}
writer.write("${L}${L}${L}: ${T}${L};", memberPrefix, memberName, optionalSuffix,
symbolProvider.toSymbol(member), typeSuffix);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public enum RequiredMemberMode {
NULLABLE("nullable"),

/**
* This will dissallow members marked as {@link RequiredTrait} to be {@code undefined}.
* This will disallow members marked as {@link RequiredTrait} to be {@code undefined}.
* Use this mode with CAUTION because it comes with certain risks. When a server drops
* {@link RequiredTrait} from an output shape (and it is replaced with {@link DefaultTrait}
* as defined by the spec), if the server does not always serialize a value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void properlyGeneratesOptionalNonMessageMemberOfException() {
"export class Err extends __BaseException {\n"
+ " readonly name: \"Err\" = \"Err\";\n"
+ " readonly $fault: \"client\" = \"client\";\n"
+ " foo?: string;\n"
+ " foo?: string | undefined;\n"
+ " /**\n"
+ " * @internal\n"
+ " */\n"
Expand Down Expand Up @@ -525,7 +525,7 @@ public void generatesNonErrorStructures() {
String output = writer.toString();

assertThat(output, containsString("export interface Bar {"));
assertThat(output, containsString("foo?: string;"));
assertThat(output, containsString("foo?: string | undefined;"));
}

private StructureShape createNonErrorStructure() {
Expand Down

0 comments on commit 3988115

Please sign in to comment.