Skip to content

Support refinement of tagged unions of array shapes #6469

@arnaud-lb

Description

@arnaud-lb

Feature request

If we have some types like

@phpstan-type Pizza   array{type: 'pizza', toppings: Topping[]}
@phpstan-type Pasta   array{type: 'pasta', salsa: Salsa}
@phpstan-type Meal    Pizza|Pasta

PHPStan could be able to determine which Meal we have after looking at the type field:

/**
 * @param Meal $meal
 */
function ($meal) {
    if ($meal['type'] === 'pizza') {
        \PHPStan\dumpType($meal); // array{type: 'pizza', toppings: Topping[]} (Pizza)
    } else {
        \PHPStan\dumpType($meal); // array{type: 'pasta', salsa: Salsa} (Pasta)
    }
}

https://phpstan.org/r/cacfda2a-94de-4bb6-8da6-2c0d36732967

Flow documentation on disjoint unions: https://flow.org/en/docs/types/unions/#toc-disjointo-object-unions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions