Skip to content

Conversation

@Livijn
Copy link
Contributor

@Livijn Livijn commented Jan 23, 2026

Summary

This PR adds support for detecting and handling nullable relationships in model:typer.

Problem

Previously, model:typer could not detect when a relationship method had a nullable return type. For example:

public function listing(): BelongsTo|Listing|null
{
    return $this->belongsTo(Listing::class)->withTrashed();
}

This would generate listing: Listing instead of listing: Listing | null.

Solution

  • Modified ModelInspector to detect nullable return types from relationship methods
  • Added isReturnTypeNullable() method that checks for:
    • Union types containing null (e.g., BelongsTo|Listing|null)
    • Nullable named types (e.g., ?BelongsTo)
  • Updated WriteRelationship to generate Type | null for nullable relations
  • Added comprehensive tests for the new functionality

Example

Input (PHP):

public function listing(): BelongsTo|Listing|null
public function breeder(): BelongsTo|Breeder|null
public function seller(): BelongsTo|User

Output (TypeScript):

listing: Listing | null  // Nullable
breeder: Breeder | null   // Nullable
seller: User              // Required

Changes

  • src/Overrides/ModelInspector.php: Override getRelations() to detect nullable return types
  • src/Actions/WriteRelationship.php: Handle nullable relations with | null syntax
  • test/Tests/Feature/Actions/WriteRelationshipTest.php: Add tests for nullable relations

Tests

All 116 tests passing, including 4 new tests for nullable relations.

- 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
@tcampbPPU
Copy link
Member

hi @Livijn thanks for the PR!
Could you add a test for this for example in your comment on how it should be generated?

Output (TypeScript):

listing: Listing | null  // Nullable
breeder: Breeder | null   // Nullable
seller: User              // Required

i'd just like to reassure any changes in the future still adhere to this new functionality

Thanks again for your contributions 🍻

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.

2 participants