Skip to content

Interpret type from already defined @template Generic which is itself a Generic #9053

Closed Answered by ondrejmirtes
alexander-schranz asked this question in Support
Discussion options

You must be logged in to vote

I looked at your example and implemented the one missing piece - a way to extract template type from an existing type, an equivalent to calling Type::getTemplateType(): https://apiref.phpstan.org/1.10.x/PHPStan.Type.Type.html#_getTemplateType

So working code now looks like this:

/**
 * @template-covariant T of ModelInterface
 */
class Helper
{
	/**
	 * @param T $model
	 */
	public function __construct(private ModelInterface $model)
	{}

	/**
	 * @return template-type<T, ModelInterface, 'TChild'>
	 */
	public function getFirstChildren(): ChildInterface
	{
		$firstChildren = $this->model->getChildren()[0] ?? null;

		if (!$firstChildren) {
			throw new \RuntimeException('No first child found.'

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@JanTvrdik
Comment options

@JanTvrdik
Comment options

@ondrejmirtes
Comment options

@alexander-schranz
Comment options

@ondrejmirtes
Comment options

Answer selected by alexander-schranz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
3 participants