Skip to content

[13.x] Add generic return type to InvokableComponentVariable - #62018

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
alies-dev:types/invokable-component-variable-generics
Oct 14, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
alies-dev:types/invokable-component-variable-generics

Conversation

@alies-dev

@alies-dev alies-dev commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Blade components pass their zero-argument public methods to views wrapped in \Illuminate\View\InvokableComponentVariable instances, and so far static analysis tools couldn't infer return types of such InvokableComponentVariable, so I'm proposing to use @template syntax for InvokableComponentVariable.

PR adds a single covariant template for the resolved value and declares it as a mixin, so a view can state what a component method returns and both calling it and proxied access are typed:

@php /** @var \Illuminate\View\InvokableComponentVariable<\Illuminate\Support\Collection<int, \App\Models\Plan>> $plans */ @endphp

{{ $plans->count() }} plans {{-- int instead of an undefined method --}}

@foreach ($plans() as $plan)
    {{ $plan->name }} {{-- $plan is App\Models\Plan instead of mixed --}}
@endforeach

The change is PHPDoc only. The template defaults to mixed, so existing unparameterized InvokableComponentVariable annotations keep working.

tools

Additionally bladestan may implement custom handlers to specify more narrow return types instead of mixed; psalm/plugin-laravel already does.

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@alies-dev
alies-dev marked this pull request as ready for review October 14, 2026 14:43
@alies-dev alies-dev changed the title [13.x] Add generic return type to InvokableComponentVariable [13.x] Add generic return type to InvokableComponentVariable Oct 14, 2026
alies-dev added a commit to psalm/psalm-plugin-laravel that referenced this pull request Oct 14, 2026
Declares `@mixin TValue` on the stub, so `__call` forwarding to the
resolved value is typed, matching the mixin added upstream in
laravel/framework#62018. Property reads stay sealed: Psalm does not
resolve templated mixins for property fetches.
@taylorotwell
taylorotwell merged commit 5cc36e3 into laravel:13.x Oct 14, 2026
59 of 64 checks passed
binaryfire added a commit to hypervel/components-backup that referenced this pull request Oct 26, 2026
Apply laravel/framework#62018 (5cc36e3233), including its complete type fixture. Component variables preserve the resolver result type through invocation and method forwarding.

The deferred display contract incorrectly restricted raw resolver results to strings and Htmlable objects, rejecting numeric and nullable component values before escaping could handle them. Return the original unconstrained resolver value. Normalize numeric values to strings only in the default HTML encoder, preserving values passed to custom encoders. Remove redundant test cleanup already owned by the framework subscriber.

Regression tests reproduce both TypeErrors before correction. Formatting, full source and type analysis, the View suite and HTML encoder tests pass.
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