Skip to content

Commit

Permalink
Make typescript output optional fields for the never types
Browse files Browse the repository at this point in the history
  • Loading branch information
enoua5 committed Oct 2, 2024
1 parent 5834355 commit 4e57fbd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
8 changes: 4 additions & 4 deletions strawberry/codegen/plugins/typescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ def _print_oneof_object_type(self, type_: GraphQLObjectType) -> str:
field_row = self._print_oneof_field(field)
else:
# ... and the rest set to `never` to prevent multiple from being set
field_row = f"{field.name}: never"
option_fields.append(textwrap.indent(field_row, " " * 4))
options.append("{\n" + ",\n".join(option_fields) + "\n}")
field_row = f"{field.name}?: never"
option_fields.append(field_row)
options.append("{ " + ", ".join(option_fields) + " }")

# Union all the options together
all_options = " | ".join(options)
all_options = "\n | ".join(options)

return f"type {type_.name} = {all_options}"

Expand Down
9 changes: 2 additions & 7 deletions tests/codegen/snapshots/typescript/oneof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ type OneOfTestResult = {
one_of: string
}

type OneOfInput = {
a: string,
b: never
} | {
a: never,
b: string
}
type OneOfInput = { a: string, b?: never }
| { a?: never, b: string }

type OneOfTestVariables = {
value: OneOfInput
Expand Down
9 changes: 2 additions & 7 deletions tests/codegen/snapshots/typescript/oneof_typename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ type OneOfTypenameTestResult = {
alias: OneOfTypenameTestResultOneOfTypenamePerson
}

type OneOfInput = {
a: string,
b: never
} | {
a: never,
b: string
}
type OneOfInput = { a: string, b?: never }
| { a?: never, b: string }

type OneOfTypenameTestVariables = {
value: OneOfInput
Expand Down

0 comments on commit 4e57fbd

Please sign in to comment.