Skip to content

Support for optional template syntaxΒ #7105

@VincentLanglet

Description

@VincentLanglet

Feature request

When describing an array, you can write

array => Reported as an error level 6
array<string> => no errors
array<string, string> => no errors

If I wand to create a similar generic,

/**
 * @phpstan-template TKey of array-key
 * @phpstan-template TValue
 */
class MyArray {}

I didn't find a way to make a template param optionnal,
so MyArray<Foo> is giving an error because I does not specify all template types

A syntax like

/**
 * @phpstan-template ?TKey of array-key
 * @phpstan-template TValue
 */
class MyArray {}

could be useful.

In order to avoid issue with

/**
 * @phpstan-template ?T1
 * @phpstan-template T2
 * @phpstan-template ?T3
 */

we might decide that all the optional template should be last, and are filled in the order they are defined.

/**
 * @phpstan-template T1
 * @phpstan-template ?T2
 * @phpstan-template ?T3
 */

Foo<T1> => Valid
Foo<T1, T2> => Valid
Foo<T1, T2, T3> => Valid
Foo<T1, T3> => Invalid

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions