psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Conditional types comparison works in a wrong way in assertions

Open fluffycondor opened this issue 2 years ago • 3 comments

https://psalm.dev/r/52685a5fda

For some reason string|int is string makes true.

fluffycondor avatar Aug 18 '22 12:08 fluffycondor

I found these snippets:

https://psalm.dev/r/52685a5fda
<?php

/**
 * @template T
 * @param T $foo
 * @psalm-assert-if-true (T is string ? numeric-string : int) $foo
 */
function foo(string|int $foo): bool
{
    return true;
}

function bar(string|int $bar): void
{
    if (foo($bar)) {
        /** @psalm-trace $bar */; # it should be numeric-string|int here
    }
}
Psalm output (using commit afe85fa):

INFO: Trace - 16:33 - $bar: numeric-string

psalm-github-bot[bot] avatar Aug 18 '22 12:08 psalm-github-bot[bot]

https://psalm.dev/r/9528b9da34 int|string is int|string makes false.

fluffycondor avatar Aug 18 '22 12:08 fluffycondor

I found these snippets:

https://psalm.dev/r/9528b9da34
<?php

/**
 * @template T
 * @param T $foo
 * @psalm-assert-if-true (T is int|string ? numeric-string|int : (T is string ? numeric-string : int)) $foo
 */
function foo(int|string $foo): bool
{
    return true;
}

function bar(int|string $bar): void
{
    if (foo($bar)) {
        /** @psalm-trace $bar */;
    }
}
Psalm output (using commit afe85fa):

INFO: Trace - 16:33 - $bar: numeric-string

psalm-github-bot[bot] avatar Aug 18 '22 12:08 psalm-github-bot[bot]