Skip to content

refactor IsNumericLike and fix UnionMin, UnionMax types #1192

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
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

benzaria
Copy link
Contributor

@benzaria benzaria commented Jun 17, 2025

  • refactor: remove unnecessary condition for IsNumericLike that cosed an error and add cases for never/any.
  • fix: behavior of UnionMin to UnionMax returning 0 for never , any for number, erroring for any and Infinity.
  • add: introduce IsAnyOrNever type alternative for IfNotAnyOrNever. (internal)

Original behavior:

type A = IsNumericLike<'1.2.3'>
//=> true

type T = UnionMax<never>
//=> 0

type T = UnionMax<any>
//=> any // Error: infinite

Current behavior:

type A = IsNumericLike<'1.2.3'>
//=> false

type T = UnionMax<never>
//=> never

type T = UnionMax<any>
//=> any

@benzaria benzaria changed the title refactor: remove unnecessary condition for IsNumericLike refactor IsNumericLike and fix UnionMin types Jun 17, 2025
fix: match behavior of `UnionMin` to `UnionMax` returning `0` for `never`
@benzaria benzaria force-pushed the numeric-like/union-min branch from 29d7761 to ca7eca2 Compare June 17, 2025 13:01
@benzaria benzaria force-pushed the numeric-like/union-min branch from a240e8d to 91a2378 Compare June 18, 2025 11:47
@benzaria benzaria requested a review from som-sm June 18, 2025 13:24
@benzaria
Copy link
Contributor Author

benzaria commented Jun 18, 2025

Reason to introduce IsAnyOrNever:

type CountTo<T> = count to T;

type Count1<T> = IfNotAnyOrNever<T, CountTo>;
type Count2<T> = IsAnyOrNever<T> extends true ? T : CountTo<T>;

type A = Count1<any>
//       ~~~~~~~~~~
// Error: Type instantiation is excessively deep and possibly infinite.

type B = Count2<any>
//=> any

IMO, IsAnyOrNever is a better alternative and should replace the current implementation of IfNotAnyOrNever. This is because, when using an If-style utility, TypeScript always evaluates both branches. That means even the falsy or undesired branch is computed, which can result in unnecessary work, potential type errors, or wasted computation despite it ultimately being discarded.

@benzaria benzaria changed the title refactor IsNumericLike and fix UnionMin types refactor IsNumericLike and fix UnionMin, UnionMax types Jun 18, 2025
@benzaria benzaria force-pushed the numeric-like/union-min branch from e780458 to 12f2ef4 Compare June 20, 2025 19:27
@benzaria
Copy link
Contributor Author

@som-sm should UnionMax return PositiveInfinity for number? I feel its more precise because number do include '+∞' and its the maximum number. the same for UnionMin should return '-∞'. WDYT?

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.

2 participants