Skip to content

Support for value-of<MyEnum> #7874

@MidnightDesign

Description

@MidnightDesign

Psalm doesn't support backed enums as a type argument for value-of:

enum ReservationState: string {
    case Pending = 'pending';
    case Confirmed = 'confirmed';
    case Cancelled = 'cancelled';
}

/**
 * @param value-of<ReservationState> $value
 */
function makeState(string $value): ReservationState {
    return ReservationState::from($value);
}

makeState(ReservationState::Pending->value); // Works
makeState('confirmed'); // Works
makeState('foo'); // Doesn't work

https://psalm.dev/r/1277567ae5

Expected:
No errors

Actual:
ERROR: InvalidDocblock - 10:11 - Untemplated value-of param ReservationState should be an array in docblock for makeState

PHPStan supports this use of value-of.

(I've searched the existing issues for this, but surprisingly, I couldn't find one. I'm sorry if I've missed it.)

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