Skip to content

Loose equality numeric == string narrows numeric to numeric-string #14053

Closed

Description

@orklah

Loose equality between numeric and string narrows the numeric side to numeric-string, but 1 == '1' is true, so an int/float can pass the check.

<?php

/**
 * @param numeric $n
 */
function f($n, string $s): void {
    if ($n == $s) {
        /** @psalm-trace $n */
    }
}

https://psalm.dev/r/5c3686abb3

Expected: $n: numeric (no narrowing to string: == juggles types).

Actual:

INFO: Trace - 8:0 - $n: numeric-string

Spotted while working on #13957 (PR #14037), which does not cover it.

Activity

  1. psalm-github-bot commented on Nov 17, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/5c3686abb3
    <?php
    
    /**
     * @param numeric $n
     */
    function f($n, string $s): void {
        if ($n == $s) {
            /** @psalm-trace $n */
        }
    }
    Psalm output (using commit 2caf523):
    
    INFO: Trace - 8:0 - $n: numeric-string
    
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions