Skip to content

Using a template type/s to specify key/s in a returned array shape #15145

Description

@dantleech

Feature request

There is a method that returns a subset of another array shape and has the following signature:

/**
 * @phpstan-type config array{"string1":array{one:int},"string2":array{two:int}}
 */
class SomeClass
{
   /**
     * @template TKey of string
     * @param TKey $key
     *
     * @return array{TKey: config[TKey]}
     */
    public function config(string $key): array
    {}
}

Currently PHPStan doesn't understand the template type when used as an array key.

The expected result would be:

$subset = $config->get('string1');
\PHPStan\dumpType($subset); // array{string1:array{one:int}}

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

Always happy to see that PHPStan handles more than I expect it to :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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