Skip to content

Enhance comparison types to support bigint comparison#1449

Open
Emiyaaaaa wants to merge 5 commits into
sindresorhus:mainfrom
Emiyaaaaa:feat/compare-bitint
Open

Enhance comparison types to support bigint comparison#1449
Emiyaaaaa wants to merge 5 commits into
sindresorhus:mainfrom
Emiyaaaaa:feat/compare-bitint

Conversation

@Emiyaaaaa

Copy link
Copy Markdown
Collaborator

@Emiyaaaaa
Emiyaaaaa requested review from sindresorhus and som-sm and removed request for som-sm June 5, 2026 09:48
@sindresorhus

Copy link
Copy Markdown
Owner
  1. The nonliteral bigint fallback runs before the infinity handling, so comparisons where one side is broad bigint and the other side is PositiveInfinity/NegativeInfinity widen to boolean, even though the answer is fixed. For example, GreaterThan<PositiveInfinity, bigint> should be true, GreaterThan<bigint, PositiveInfinity> should be false, and LessThan<bigint, PositiveInfinity> should be true, but they all currently come out as boolean. Moving the broad bigint fallback until after the infinity cases should keep the existing precision.

  2. The tests add mixed bigint/number support, but that path is not correct for decimal number literals. For example, GreaterThan<2n, 1.5> currently resolves to false, and GreaterThan<1.5, 2n> resolves to true, because the positive numeric string comparison treats "1.5" as longer than "2". The issue only asked for bigint-to-bigint support, so the simplest fix is probaly to drop mixed number/bigint support from this PR, or make the contract explicitly integer-only if that is intended.

Also the public docs still only talk aboutt number and non-literal number, so they should be updated to mention bigint and broad bigint behavior too

@Emiyaaaaa

Copy link
Copy Markdown
Collaborator Author

DONE

@som-sm

som-sm commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Umm... GreaterThan<2n, 1.5> still returns false, and GreaterThan<1.5, 2n> still returns true?

@Emiyaaaaa

Copy link
Copy Markdown
Collaborator Author

Umm... GreaterThan<2n, 1.5> still returns false, and GreaterThan<1.5, 2n> still returns true?

GreaterThan previously only supported integers; I have updated the type description.

expectType<GreaterThanOrEqual<PositiveInfinity, bigint>>({} as true);
expectType<GreaterThanOrEqual<bigint, PositiveInfinity>>({} as false);
expectType<GreaterThanOrEqual<NegativeInfinity, bigint>>({} as false);
expectType<GreaterThanOrEqual<bigint, NegativeInfinity>>({} as true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests for PositiveInfinity/NegativeInfinity and number.

Comment thread test-d/greater-than.ts
Comment on lines +68 to +71
expectType<GreaterThan<PositiveInfinity, bigint>>({} as true);
expectType<GreaterThan<bigint, PositiveInfinity>>({} as false);
expectType<GreaterThan<NegativeInfinity, bigint>>({} as false);
expectType<GreaterThan<bigint, NegativeInfinity>>({} as true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine to return boolean in all these cases. PositiveInfinity and NegativeInfinity are anyway not that common, and returning boolean isn't inaccurate, it's just imprecise.

Also, if we handle these cases, then GreaterThan<PositiveInfinity, number> should also return false, which it currently doesn't (returns boolean currently).

@som-sm som-sm Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to keep the implementation simple, and just return bigint if bigint extends A | B. And remove the IsFinite type.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numeric comparison types: support for bigints?

3 participants