TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

No error on unconstrained type parameter in `>` comparison

Open cakoose opened this issue 2 years ago • 4 comments

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.

cakoose avatar Sep 02 '22 06:09 cakoose