Skip to content

Conversation

@Livijn
Copy link

@Livijn Livijn commented Jan 23, 2026

This PR adds support for properly handling MorphTo relations with union type return types.

Problem

When a model has a MorphTo relation with a union type return type, model:typer was incorrectly generating the TypeScript type as the current model class instead of the union type.

Solution

  • Updated ModelInspector to extract union types from MorphTo relation return types
  • Updated WriteRelationship to properly output union types for MorphTo relations
  • Added comprehensive tests for the new functionality

Changes

  • src/Overrides/ModelInspector.php: Added extractMorphToRelatedModels() method
  • src/Actions/WriteRelationship.php: Added logic to detect and output MorphTo union types
  • Added test model MorphRelation with MorphTo relation
  • Added tests in RunModelInspectorTest and WriteRelationshipTest
  • Updated GetModelsTest to include the new test model

- Detect nullable return types in relationship methods
- Mark nullable relations as optional (?) in TypeScript output
- Handle union types containing null (e.g., BelongsTo|Listing|null)
- Handle single nullable types (e.g., ?BelongsTo)
- Changed nullable relations to use 'Type | null' instead of 'Type?'
- More explicit and follows TypeScript best practices
- Optional relations (via config) still use '?' syntax
- Add tests for WriteRelationship nullable handling
- Add tests for ModelInspector nullable detection
- Make isReturnTypeNullable public for testability
- Test nullable union types (e.g., BelongsTo|Listing|null)
- Test nullable named types (e.g., ?BelongsTo)
- Test non-nullable relations
- Test nullable plural relations
- Remove tests for isReturnTypeNullable (implementation detail)
- Keep isReturnTypeNullable protected (not public API)
- Keep tests for WriteRelationship (public behavior)
- All 116 tests passing
@Livijn
Copy link
Author

Livijn commented Jan 23, 2026

I think this pr includes the #113 as well

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