lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.45k stars 494 forks source link

Enhance Nullable Union Type Inference through Strict Mode #1475

Closed antoniel closed 11 months ago

antoniel commented 1 year ago

Summary

This PR addresses issue #1474, aiming to improve the type inference mechanism for nullable union types in tsoa. By enabling TypeScript's strict mode, we significantly enhance the accuracy of type definitions and OpenAPI Schemas generated by the library.

Changes

Impact

How to Test

  1. Pull the changes and install updated dependencies.
  2. Run the existing unit tests to validate that the change is working as expected.
  3. Manually create a route using a nullable union type and confirm that the generated OpenAPI schema is accurate.

Context


WoH commented 1 year ago

Does setting it in the tsconfig of your tsoa's config file not work? See https://github.com/lukeautry/tsoa/blob/master/packages/runtime/src/config.ts#L41C14-L41C14

I am worried this might have unintended consequences when we run this on strict: false codebases and break typing completely. That said, maybe strict: true is by now common enough to test it.

antoniel commented 1 year ago

Thank you for your suggestion! I did try setting it in the tsoa's config file, and it seems to work well. I agree that ensuring compatibility is essential. However, in scenarios where the OpenAPI file serves as a blueprint to generate validation schemas using tools like Zod, maintaining accurate type representation is critical. Having just string instead of string | null could lead to serious issues.

antoniel commented 1 year ago

Maybe we can add explicitly in the documentation that setting the tsoa as strict in their config files is highly recommended, even if the tsconfig is not strict.

WoH commented 1 year ago

That would be highly appreciated.