Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return type: subtype of type #10898

Open
distantnative opened this issue Apr 11, 2024 · 1 comment
Open

Return type: subtype of type #10898

distantnative opened this issue Apr 11, 2024 · 1 comment

Comments

@distantnative
Copy link

How can I type hint of the subtype/template of a type/template?

https://psalm.dev/r/58e26f6817

In this example, how can I hint that the return value will be of type TA corresponding to whichever TClass type $item is?

Copy link

I found these snippets:

https://psalm.dev/r/58e26f6817
<?php

class Element {}

/**
 * @template TA
 */
class A {
  /**
   * @param TA $item
   */
  public function __construct(
    protected $item
  ) {}

  /**
   * @return TA
   */
  public function get()
  {
    return $this->item;  
  }
}

/**
 * @extends A<Element>
 */
class B extends A {
}


/**
 * @template TClass
 */
class C {
  /**
   * @param TClass $item
   */
  public function __construct(
    protected $item
  ) {}

  /**
   * @return TClass[TA]
   */
  public function foo() {
    return $this->item->get();
  }
}
Psalm output (using commit ef3b018):

ERROR: InvalidDocblock - 46:3 - Unrecognised template param TA in docblock for C::foo

INFO: MixedMethodCall - 47:25 - Cannot determine the type of $this->item when calling method get

INFO: MissingReturnType - 46:19 - Method C::foo does not have a return type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant