Skip to content

No error on unconstrained type parameter in > comparisonΒ #50603

Open

Description

Bug Report

πŸ”Ž Search Terms

object is possibly undefined 4.8

πŸ•— Version & Regression Information

  • This changed between versions 4.7.4 and 4.8.2

⏯ Playground Link

TS playground on 4.8.0-beta link.

(The playground doesn't seem to have 4.8.x releases, but I tested on 4.8.0-beta and nightly (4.9.0-dev.XXXXXXX).)

πŸ’» Code

export function min<T>(it: Iterable<T>): T | null {
    let result: T | null = null;
    for (const v of it) {
        if (result === null || result > v) {
            result = v;
        }
    }
    return result;
}
error TS2532: Object is possibly 'undefined'.

        if (result === null || result < v) {
                               ~~~~~~

On that line, hovering over the first result shows T | null. Hovering over the second result shows T & ({} | undefined).

πŸ™ Actual behavior

I get a type error.

πŸ™‚ Expected behavior

No error.

Activity

  1. jakebailey commented on Sep 2, 2022

    @jakebailey
    Member

    This is expected behavior in 4.8. See this section in the blog post: https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#unconstrained-generics-no-longer-assignable-to

    You probably want to change your type variable to be constrained to {} by writing T extends {}, or check for undefined.

  2. cakoose commented on Sep 2, 2022

    @cakoose
    Author

    Ah, interesting! Where's the {} requirement coming from, the comparison?

    For example, I'm confused why there's no error here:

    function f<T>(a: T, b: T): boolean {
        return a > b;
    }
  3. jakebailey commented on Sep 2, 2022

    @jakebailey
    Member

    Honestly, I didn't even notice that the operator you used was >. It does seem weird that the above compiles, yet, this does not.

    function f<T>(a: T, b: T): boolean {
        if (a === undefined} {
            return true;
        }
    
        return a > b;
    }

    In that it's odd that we allow > and < on an unconstrained type variable like this.

  4. andrewbranch commented on Sep 2, 2022

    @andrewbranch
    Member

    The current behavior is clearly inconsistent. If you can’t use undefined in a greater than or less than check, you shouldn’t be able to use an unconstrained type parameter either.

  5. changed the title [-]New error in TS 4.8: Object is possibly 'undefined' (after null check on `T | null`)[/-] [+]No error on unconstrained type parameter in `>` comparison[/+] on Sep 2, 2022
  6. ahejlsberg commented on Jul 29, 2024

    @ahejlsberg
    Member

    Note that the issue reported here also exists for the +, -, and ~ unary operators. For example:

    function foo<T>(x: T) {
        +x;  // Ok
        if (x === undefined) return;
        +x;  // Error
    }

    With #59437, errors are reported in both cases above.

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

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions