Skip to content

Chainable types do not work #12

Open
@bendavies

Description

@bendavies

the following code:

<?php

declare(strict_types=1);

use Assert\Assert;

class Result
{
    private ?float $percent;

    private function __construct()
    {
    }

    /** @param array<string, mixed> $payload */
    public static function fromQueryResults(array $payload): self
    {
        Assert::that($payload['percent'])
            ->nullOr()
            ->string()
            ->numeric();

        $floatOrNull = static fn (?string $value): ?float => null === $value ? null : (float) $value;

        $instance = new self();
        $instance->percent = $floatOrNull($payload['percent']);

        return $instance;
    }
}

Produces the error:
Parameter #1 $value of closure expects string|null, float|int|string|null given.

The type of $payload['percent'] should be infered to string|null, not float|int|string|null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions