Repository: TypeScript
Issue: Add numeric predicate expression literal types
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms 

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

number template literal type
template number literal type
expression number literal types
bounded numbers

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [✅] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [✅] This wouldn't change the runtime behavior of existing JavaScript code
* [✅] This could be implemented without emitting different JS based on the types of the expressions
* [✅] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [✅] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->
I'd like to see number expression literal types. Like string template literal types but for numbers. But since numbers are computed through expressions, it would be more helpfull to have numbers matching with computation expressions.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->
Typescript allows you to define template string literal types like this :  
```typescript
type OddNumberString = `${number}${1|3|5|7|9}`;
```
But cannot do the same for real numbers.
What if you could indeed declare numbers with expression literal types :  

```typescript
type OddNumber = n`(number+1) % 2 === 0`;
type EvenNumber = n`number % 2 === 0`;
```

it would match like this :  
```typescript
const odd1: OddNumber = 6; // error
const odd2: OddNumber = 5: // succeed 
```

The type system would just compute the expression and check that the result should be equal true to match a number.

Only number, bits and logic operators would be allowed inside ```n`` ``` : 
- `+`
- `-`
- `*`
- `/`
- `%`
-  `**`
- `>`
- `<`
- `==`
- `===`
- `<=`
- `>=`
- `||`
- `&&`
- `|`
- `&`
- `^`

allowing Math functions could also be doable, but not mandatory.

You could also use it with extend ternary operator :  
```typescript
type IsOddNumber<T extends number> = T extends n`(number+1) % 2 === 0` ? true : false;
```

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->
This could be used to check for non zero positive numbers :  
```typescript
type NaturalNumber = n`number>0`;
```

Or bounded numbers to express rand results :  
```typescript
type Bounded<MIN extends number,MAX extends number> = n`number>=${MIN} && number <${MAX}`;

type RandomResult = Bounded<0,1>;
```