Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Narrowing caused by Type\instance_of #7

Open
bendavies opened this issue Jul 21, 2022 · 2 comments
Open

Type Narrowing caused by Type\instance_of #7

bendavies opened this issue Jul 21, 2022 · 2 comments

Comments

@bendavies
Copy link

bendavies commented Jul 21, 2022

Hi,

Given a variable that the type is already known:

$foo = Type\instance_of(BarInterface::class)->assert($foo);
$foo = Type\instance_of(FooInterface::class)->assert($foo);

Then type of $foo will become FooInterface, but I would expect BarInterface&FooInterface

What do you think?

Thanks

@herndlm
Copy link

herndlm commented Jul 21, 2022

assert makes it throw in case the type does not match I presume?

Most likely the extension cannot handle the assert yet, since phpstan itself handles that just fine. See e.g. https://phpstan.org/r/13ea6cd2-061b-47ac-925e-ee1f50c86c0e. Without assert support it would kind of translate to the second block and therefore loose type information in the scope.

@bendavies
Copy link
Author

Using a class and an Interface was a bad example on my part.

Using 2 interfaces is the real use case.

I've edited the OP

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

No branches or pull requests

2 participants