Skip to content

Add nullable strategy (null/undefined) support - #284

Open
gluschenko wants to merge 1 commit into
nenoNaninu:mainfrom
gluschenko:nullable-behaviour
Open

gluschenko wants to merge 1 commit into
nenoNaninu:mainfrom
gluschenko:nullable-behaviour

Conversation

@gluschenko

Copy link
Copy Markdown

This pull request introduces a new option to control how nullable types are represented in the generated TypeScript code. Users can now choose between representing nullable members with undefined (the previous default) or with null. This affects both the CLI, the code generation logic, and is reflected in the documentation and tests.

Nullable Type Representation Support

  • Added a --nullable CLI option to select between undefined and null for representing nullable members in generated TypeScript code. The default remains undefined, but users can now opt-in to explicit null unions. (README.md, src/Tapper.Generator/App.cs)
  • Updated the internal options (TranspilationOptions, ITranspilationOptions) and code generation logic to support and propagate the new NullableStrategy setting throughout the transpilation process. (src/Tapper/ITranspilationOptions.cs, src/Tapper/TranspilationOptions.cs)
  • Modified type mappers and translators to emit the correct TypeScript union (| undefined or | null) or optional property marker based on the selected nullable strategy. (src/Tapper/TypeMappers/NullableStructTypeMapper.cs, src/Tapper/TypeMappers/TupleTypeMapper.cs, src/Tapper/TypeTranslators/DefaultMessageTypeTranslator.cs)

Documentation and Usability

  • Updated the README.md to document the new --nullable option, including usage examples and the effect on generated TypeScript code. Also clarified the TypeScript mapping for System.Nullable<T>.

Testing

  • Added new tests to verify that nullable reference types and tuples are correctly represented as null unions when the NullableStrategy.Null option is used. (tests/Tapper.Tests/InheritanceTests.cs, tests/Tapper.Tests/TupleTest.cs) Introduce a NullableStrategy option and propagate it through the CLI and transpilation options so generated TypeScript can use either undefined (default) or null for nullable members. Adds a --nullable CLI flag, a NullableStrategy enum, and helper methods (GetNullableUnionLiteral, UseOptionalPropertyForNullableReferenceTypes) on ITranspilationOptions. Type mappers (NullableStructTypeMapper, TupleTypeMapper) and the message type translator now respect the chosen strategy by emitting either optional properties or explicit unions with null. README updated with usage and examples, and unit tests added to verify null emission for tuples and nullable reference types.

Introduce a NullableStrategy option and propagate it through the CLI and transpilation options so generated TypeScript can use either `undefined` (default) or `null` for nullable members. Adds a --nullable CLI flag, a NullableStrategy enum, and helper methods (GetNullableUnionLiteral, UseOptionalPropertyForNullableReferenceTypes) on ITranspilationOptions. Type mappers (NullableStructTypeMapper, TupleTypeMapper) and the message type translator now respect the chosen strategy by emitting either optional properties or explicit unions with `null`. README updated with usage and examples, and unit tests added to verify `null` emission for tuples and nullable reference types.
@gluschenko

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant