Skip to content

Generic typing of Option::fromValue results in Option<valueType|null> #63

Closed

Description

@arnaud-lb

Option::fromValue uses the same type parameter C for the $value and $noneValue parameters. As a result, the type of C is inferred to a union of the types of $value and $noneValue:

https://phpstan.org/r/af2d3270-93fe-463a-9a29-4dd584012cc3

Typing $noneValue to mixed fixes the problem:

https://phpstan.org/r/c105ea3e-21e2-4dbc-a119-36fd1154ef3e

I believe that this would be ok since $noneValue doesn't contribute to type of Option.

Activity

  1. GrahamCampbell commented on Aug 24, 2026

    @GrahamCampbell
    Collaborator

    Fixed in 1.10.0. fromValue() now types $noneValue as mixed, so the value type is inferred from $value alone and you get back Option<T> rather than Option<T|null> — exactly as you suggested. Thanks for the clear report and playground links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions