Skip to content

Error when trying to infer type with Option::filter() into a function that requires (Closure(Tv): T) #27

@devnix

Description

@devnix

In relation to #25 and the conversation in #26, I have a strange case that I've been able to replicate in a unit test on this repository:

Parameter #2 $function of function Psl\Dict\map expects Closure(string): Psl\Option\Option<Tv&non-empty-string>, Closure(string): Psl\Option\Option<non-empty-string> given.
\Psl\Dict\map(
	\Psl\Type\dict(\Psl\Type\array_key(), \Psl\Type\string())
		->coerce($data),
	function (string $value): \Psl\Option\Option {
		return \Psl\Option\Option::some($value)
			->filter(\Psl\Type\non_empty_string()->matches(...));
	}
);

I've thought about doing an intersection of $originalType and $refinedType at https://github.com/php-standard-library/phpstan-extension/blob/2.0.x/src/Option/OptionFilterReturnTypeExtension.php#L66 but it worsens:

Parameter #2 $function of function Psl\Dict\map expects Closure(string): Psl\Option\Option<(string&Tv)&(Tv&non-empty-string)>, Closure(string): Psl\Option\Option<non-empty-string&string> given.

Also I've been diving into the different APIs but I've not been able to come up with a solution, and I'm not sure if I should dig more or if it's a current bug in the type system that I should assume it's not fixable on this plugin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions