Skip to content

No way to annotate array key template #2568

@muglug

Description

@muglug

Bug report

/**
 * @template T as int|string
 *
 * @param array<T, mixed> $arr
 * @return array<int, T>
 */
function my_array_keys($arr) {
	return array_keys($arr);
}

$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);

https://phpstan.org/r/b92e2661-21cd-4182-b987-312171de77fd

Expected output

No issues

Actual

Function my_array_keys() should return array<int, T> but returns array<int, (int|string)>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions