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

[Feature Request] Support for Digit and Digits #39

Open
brandonmcconnell opened this issue Feb 17, 2023 · 0 comments
Open

[Feature Request] Support for Digit and Digits #39

brandonmcconnell opened this issue Feb 17, 2023 · 0 comments

Comments

@brandonmcconnell
Copy link

brandonmcconnell commented Feb 17, 2023

It would be helpful to be able to specify Digits and Digit types.

Definitions

import * as H from 'hotscript'

type Digits<T extends number> = H.Eval<H.Unions.Range<
  H.Eval<H.N.Power<10, H.Eval<H.N.Sub<T, 1>>>>,
  H.Eval<H.N.Sub<H.Eval<H.N.Power<10, T>>, 1>>
>> | (T extends 1 ? 0 : never);

type Digit = Digits<1>;

Usages

const some1DigitNumber: Digit = 5; // ✅
const some1DigitNumberString: `${Digit}` = "5"; // ✅
const some2DigitNumber: Digits<2> = 99; // ✅
const some2DigitNumberString: `${Digits<2>}` = "99"; // ✅

TypeScript Playground: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAKjgQwM5wBJwGZQiOAcgAsIZUBjKYMGQg…


UPDATE: I've refined the example to include the recently supported Unions.Range.

@brandonmcconnell brandonmcconnell changed the title Support for Digit and Digits [Feature Request] Support for Digit and Digits Feb 17, 2023
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

No branches or pull requests

1 participant