Skip to content
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

Proposal: Enumerate and IntRange #669

Closed
lucasteles opened this issue Aug 24, 2023 · 2 comments · Fixed by #707
Closed

Proposal: Enumerate and IntRange #669

lucasteles opened this issue Aug 24, 2023 · 2 comments · Fixed by #707
Labels
help wanted Extra attention is needed type addition

Comments

@lucasteles
Copy link
Contributor

Useful for when we are working with restrict numeric values

definition:

type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
  ? Acc[number]
  : Enumerate<N, [...Acc, Acc['length']]>

type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>> | T

So it can be used:

type ValidAmount = Enumerate<4> //  0 | 1 | 2 | 3
type WeekDay = IntRange<1, 7> // 1 | 2 | 3 | 4 | 5 | 6 | 7
@sindresorhus
Copy link
Owner

Related to #123

@sindresorhus
Copy link
Owner

This is accepted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants