Description
When object properties in the schema are optional, like so:
"/search/photos": {
"get": {
"parameters": [
{
"name": "orientation",
"in": "query",
"schema": {
"type": "string"
}
}
],
"responses": {}
}
},
… openapi-typescript will generate a type with optional properties, like so:
"/search/photos": {
get: {
parameters: {
query: {
orientation?: string;
};
};
};
};
When we later try to use this type e.g. using openapi-fetch, if exactOptionalPropertyTypes is enabled we will get an error when passing explicit undefined:
const fetch = (orientation: string | undefined) =>
fetchUnsplash.GET('/search/photos', { params: {
// Property 'query' is missing in type '{ orientation: string | undefined;
// }' but required in type '{ orientation?: string; }'.
query: {
orientation,
}
}});
Proposal
Add | undefined to optional property types to allow explicit undefineds. For example:
-orientation?: string;
+orientation?: string | undefined;
This is also the approach taken by React types e.g. to allow className={stringOrUndefined}: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0691ed151006d619a77d5ad8104d444906a99502/types/react/index.d.ts#L2670
Extra
Description
When object properties in the schema are optional, like so:
… openapi-typescript will generate a type with optional properties, like so:
When we later try to use this type e.g. using openapi-fetch, if
exactOptionalPropertyTypesis enabled we will get an error when passing explicitundefined:Proposal
Add
| undefinedto optional property types to allow explicitundefineds. For example:This is also the approach taken by React types e.g. to allow
className={stringOrUndefined}: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0691ed151006d619a77d5ad8104d444906a99502/types/react/index.d.ts#L2670Extra