Skip to content

Commit

Permalink
fix: Prevent unnecessary use of & {} and use NonNullable instead
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Sep 3, 2024
1 parent c6f491e commit 5359554
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@denosaurs/typefetch",
"version": "0.0.12",
"version": "0.0.13",
"exports": {
".": "./main.ts"
},
Expand Down
20 changes: 16 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ export function escapeObjectKey(key: string): string {
return `"${key}"`;
}

/**
* Prevents narrowing of string literal union to string.
*/
function toSafeUnionString(
type: string | undefined,
_: number,
types: (string | undefined)[],
): string | undefined {
if (type === "string" && types.length > 1) {
return "NonNullable<string>";
}
return type;
}

export function toSchemaType(
document: OpenAPI.Document,
schema?:
Expand Down Expand Up @@ -95,8 +109,7 @@ export function toSchemaType(
if (schema.oneOf) {
return schema.oneOf
.map((schema) => toSchemaType(document, schema))
// Prevents narrowing of string literal union to string
.map((type) => type === "string" ? "(string & {})" : type)
.map(toSafeUnionString)
.filter(Boolean)
.join("|");
}
Expand All @@ -117,8 +130,7 @@ export function toSchemaType(

return schema.anyOf
.map((schema) => toSchemaType(document, schema))
// Prevents narrowing of string literal union to string
.map((type) => type === "string" ? "(string & {})" : type)
.map(toSafeUnionString)
.filter(Boolean)
.join("|");
}
Expand Down

0 comments on commit 5359554

Please sign in to comment.