0

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.

2
  • 2
    I think this question is better suited for their github discussions or issues Commented Jul 23, 2024 at 21:41
  • @Mr.Hedgehog Yes. You're not wrong. For some reason I thought the Github repo was pretty inactive, but looking now, I see that I was wrong. Link to my post there: github.com/RicoSuter/NSwag/discussions/4949 Commented Jul 31, 2024 at 18:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.