Skip to content

Operator '<' cannot be applied to types 'Number' and 'number'Β #52753

Closed
@DarrenDanielDay

Description

@DarrenDanielDay

Bug Report

πŸ”Ž Search Terms

Number object
number primitive
binary operator <
ts2365
TypeScript 5.0-dev

πŸ•— Version & Regression Information

  • This changed between versions 4.9.5 and 5.0.0-dev.20230210

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const a = new Number(1);
const b = 2;
console.log(a < b)

πŸ™ Actual behavior

In TypeScript 5.0.0-dev.20230210, it reports error ts2365, but in TypeScript 4.9.5, it doesn't.

πŸ™‚ Expected behavior

I'm not sure whether this is a bug of 5.0-dev or a breaking change that which will be introduced in 5.0. If it's a breaking change by design, maybe it should be included in #51362? If not, it should not report any error since it's valid JavaScript code.

Actually, I run into this error when I try to extend Number.prototype for utility:

interface Number {
    [Symbol.iterator](): Generator<number>;
}

Number.prototype[Symbol.iterator] = function*() {
    for (let i = 0; i < this; i++) yield i;
    //              ^^^^^^^^ ts2365
}

for (const i of 10) console.log(i);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions