Replies: 1 comment 1 reply
-
You can get this with a conditional return type and another template parameter for closure return type: https://psalm.dev/r/3d49052546 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there any way to represent a function in Psalm which acts like this?
Closure():T
, call it with all other arguments passed and return its return value with typeT
Closure():T
, return the value directlyThe goal at the end of the day is to write a proper stub for the
value()
helper of Laravel which does exactly that and is often used to allow default arguments of other helpers/functions to either contain a value directly or a Closure returning the desired value.I already tried https://psalm.dev/r/8ee69ae96b and would have imagined that this works, but for some reason Psalm returns
impure-Closure():int|int
for the last variable instead of justint
as expected.I noticed that narrowing
@template T
to@template T as scalar|null
fixes the issue, but this is not really a solution asvalue
is not just limited to scalars either. It seems to me that Psalm thinks aClosure():int
could either matchClosure(...):T
orT
and does not infer correctly that as soon asT
is aClosure()
, it can never be directly returned.Beta Was this translation helpful? Give feedback.
All reactions