Skip to content

[Bug]: Method template K of key-of<T> is inferred as string instead of the literal key, so valid keys fail the upper bound #139

Description

@fdaviddpt

Version: 0.14.1

Playground (reproduces): https://typephp-php.github.io/docs/playground#code=N4IgLglmA2CmIC4QGtYE8C0B7AZgHgBUA+AAgHcoALEgkgIywFcA7AE3rRIAFYAPMWGwDOIADQgAxllbwkeAPwAHSooA6zdTInQAhgCdYACiFg9ECWAD6YNIthCAvAEYAlAG516gPQAqH+pIfbgEAW0VdARoSXBJ9PR00PBMzZgBzURIQiF5YViIAny91HTpknQsSbR0hIRIAIWrYdWAAkjbff2Y27qCuUPCdSIBpaJwSVExcQnyu7sDuRX0dEJIRgBIJ1p6i2bbFRjpocxIcFgsILC7U2DBjUwg0kg30FwQSZIfUrZIW3bmSAxgRh6LrPNAeP4AX3U0I0zA6BW4fAEwnqjTwcQSwCEsDgFlyAFlBIw3h80hkDDpWAAZCAmUn3NKQmaBHY4B46aCVXQ1EgAYUoEGg7GRglYtQaOOaMM8zFgEkoWBIhjlZH5guFhhcLgwRGutwA5Di8QJWETmIwDS4MqoQKpPCAIWJJJd2alEMBIZCgA

/** @template T of array<string, mixed> */
abstract class Base
{
    /**
     * @template K of key-of<T>
     * @param K $key
     */
    public function get(string $key): string { return $key; }
}

/** @extends Base<array{selectedMenu: string, readList: string}> */
final class Child extends Base {}

(new Child())->get('selectedMenu');

Expected: no error. 'selectedMenu' is a key of the array bound to T through @extends, so it satisfies key-of<T> (PHPStan accepts it).

Actual:

TypePHP\Exception\TypeError: Child::get(): Generic type argument string does not satisfy upper bound
key-of<array{selectedMenu: string, readList: string}> of template K

T is resolved correctly (the message shows the bound array). It looks like K is inferred from the native parameter type (string) rather than from the argument's literal value, and string is then checked against the key union.

Impact for us: ~830 reports per run on one module method (getConfiguration(string $type, ?string $key) with @template K of key-of<TConfigurationItem>), all on valid keys. We park it on our side until fixed.

Activity

  1. self-assigned this
    on Nov 25, 2026
  2. added
    bug
    on Nov 25, 2026
  3. added theissue type on Nov 25, 2026
  4. rcalicdan commented on Nov 25, 2026

    @rcalicdan
    Member

    @fdaviddpt Hello this is now resolved in v0.14.2

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

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions