Template code: https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.CodeGeneration.TypeScript/Templates/Client.ProcessResponse.Return.liquid
Generated code looks something like:
protected processGetById(response: Response): Promise<Thing> {
const status = response.status
let _headers: any = {}
if (response.headers && response.headers.forEach) {
response.headers.forEach((v: any, k: any) => (_headers[k] = v))
}
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null
let resultData200 =
_responseText === ''
? null
: JSON.parse(_responseText, this.jsonParseReviver)
result200 = Thing.fromJS(resultData200)
return result200
})
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException(
'An unexpected server error occurred.',
status,
_responseText,
_headers
)
})
}
return Promise.resolve<Thing>(null as any)
}
Doesn't asserting null as any here defeat the purpose of using TypeScript? Why wouldn't the default TS client type the function as returning Promise<Thing | null>?
When using these clients, we now have to manually remember that the functions can return null despite their TS typings.