Skip to content

No error on unconstrained type parameter in > comparison #50603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cakoose opened this issue Sep 2, 2022 · 5 comments Β· May be fixed by #59059, #59352 or #59437
Open

No error on unconstrained type parameter in > comparison #50603

cakoose opened this issue Sep 2, 2022 · 5 comments Β· May be fixed by #59059, #59352 or #59437
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@cakoose
Copy link

cakoose commented Sep 2, 2022

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.

@jakebailey
Copy link
Member

jakebailey commented Sep 2, 2022

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.

@jakebailey jakebailey added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 2, 2022
@cakoose
Copy link
Author

cakoose commented Sep 2, 2022

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;
}

@jakebailey jakebailey removed the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 2, 2022
@jakebailey
Copy link
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.

@andrewbranch
Copy link
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.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 2, 2022
@andrewbranch andrewbranch added this to the TypeScript 4.9.0 milestone Sep 2, 2022
@andrewbranch andrewbranch 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 Sep 2, 2022
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Feb 1, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 27, 2024
@ahejlsberg ahejlsberg linked a pull request Jul 26, 2024 that will close this issue
@ahejlsberg
Copy link
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