vimeo / vimeo/psalm

Generic class with static return type coming from a interface

Open
#7,204 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug static templates
Dominant language
PHP
Stars
5.9k
Forks
708
Avg merge
2d 3h
Merged PRs (30d)
25

Description

Hi,

I have defined an interface with the return type static. Afterwards I implemented a generic class that uses the interface. When I call the interface method the type is not correctly resolved. See the example:

https://psalm.dev/r/b59404794e

I fix the error by added a doc comment in the class implementation:

class Collection implements CopyInterface {
  /** @return static */                 # <--- This line solves the problem
  public function copy(): static {
    // No real implementation
    return $this;
  }
}

Is this an intended behavior?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Psalm playground example and compare the generic class implementing the interface with and without the @return static annotation. Trace how Psalm resolves static return types through interface implementations; done means the behavior is either corrected so the return type resolves without the annotation or clearly established as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.