Skip to content

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

Open
@cakoose

Description

@cakoose

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis 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