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

PositiveIntegerString type #388

Open
mohsen1 opened this issue Apr 23, 2024 · 1 comment
Open

PositiveIntegerString type #388

mohsen1 opened this issue Apr 23, 2024 · 1 comment
Labels
enhancement New feature or request v10.1

Comments

@mohsen1
Copy link

mohsen1 commented Apr 23, 2024

What

This is more of a question for advanced type experts. Would it be possible to make a type that only allows positive integers in a string? E.g. "123" and "82739283293237"

In my attempt to use template string types with many unions will hit a wall with

Expression produces a union type that is too complex to represent.(2590)

Examples

See my attempt here

My attempt for PositiveIntegerString

type Digits = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
// Problem: This pattern can not be repeated for too long...
type PositiveIntegerString = ${Digits}${Digits | ''}${Digits | ''}${Digits | ''};

function processPositiveInteger(input: PositiveIntegerString) {
console.log("Valid positive integer string:", input);
}

// Example Usage:
processPositiveInteger("3"); // OK
processPositiveInteger("323"); // OK
processPositiveInteger("1323"); // OK
processPositiveInteger("1322323233"); // Should be OK but is not
processPositiveInteger("-1"); // Error
processPositiveInteger("12.34"); // Error

@Beraliv
Copy link
Collaborator

Beraliv commented Apr 26, 2024

Hey @mohsen1, thank you for the feature request!

Currently we don't have any use cases for numbers but I am happy to add them.

Would be other related types be useful for you, eg integers, negative integers, etc?

@Beraliv Beraliv added the v10.1 label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v10.1
Projects
None yet
Development

No branches or pull requests

2 participants