Skip to content

Generic Array[Key] Shape vs Native Return Type causes subtype mismatch #13652

@zimzat

Description

@zimzat

Bug report

I encountered an oddity while trying to figure out how to work with array shapes vs functions on DTOs using generics. A method that omits the native return type and only uses phpdoc (or specifies mixed) resolves to the type expected, but once a PHP type is also specified it emits a subtype error and no longer resolves to the PHPDoc type.

PHPDoc tag @return with type array{a: string, b: bool, c: int, d: float}[TKey] is not subtype of native type bool|float|int|string.
return.phpDocType
Dumped type: bool|float|int|string

While either of these works as expected:

	/**
	 * @template TKey of key-of<Bob>
	 * @param TKey $key
	 * @return Bob[TKey]
	 */
	public function x(string $key)
	public function x(string $key): mixed

Would mixed actually be the best / correct usage here, or ...?

Code snippet that reproduces the problem

https://phpstan.org/r/65fce4bf-83a1-414e-9189-0418e945b0a4

Expected output

Dumped type: bool

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions