Phpstan: "Array shapes" with extra and optional keys problem

Created on 16 Apr 2020  路  3Comments  路  Source: phpstan/phpstan

This works as expected: https://phpstan.org/r/c11d6f2d-ce6b-4338-a7fc-6bd6695c25cd
The abc() accepts a shape with extra keys.

However, if I update parameter type of abc() to make some of its keys optional, PHPStan complains: https://phpstan.org/r/82e97898-71bf-496b-abe9-0d79f7caf757

And it complains even if the given key is optional in both parameter and argument types: https://phpstan.org/r/7ac7677c-c1f9-4393-bd44-e70ef3d5935e

Expected behaviour

An array shape can accept another array shape with extra keys and the accepting type can have its keys optional regardless of their optionality in the assigned type.

bug

All 3 comments

@ondrejmirtes This it a feature that it accepts a shape with extra keys? Why?

@JarJak Why not? :) You're saying "I need these keys to be there", it doesn't matter something extra is passed in there.

Was this page helpful?
0 / 5 - 0 ratings