https://github.com/yoav-lavi/ts-dependent-types
An experimental collection of dependent type definitions in TypeScript
https://github.com/yoav-lavi/ts-dependent-types
dependent-types typescript
Last synced: 11 days ago
JSON representation
An experimental collection of dependent type definitions in TypeScript
- Host: GitHub
- URL: https://github.com/yoav-lavi/ts-dependent-types
- Owner: yoav-lavi
- License: mit
- Created: 2022-02-05T15:19:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-14T09:18:56.000Z (about 3 years ago)
- Last Synced: 2025-03-25T04:34:55.303Z (28 days ago)
- Topics: dependent-types, typescript
- Language: TypeScript
- Homepage:
- Size: 61.5 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-dependent-types
An experimental collection of dependent type definitions in TypeScript
[Dependent types](https://en.wikipedia.org/wiki/Dependent_type) are types that rely on the value of variables rather than only on types. These can be useful to further restrict contracts.
For example:
> Given a function that retries and operation, we want to limit the number of retries to a range of 1 to 5
TypeScript supports a limited number of members for a single union and a limited recursion depth, so these types have some limitations if used multiple times in conjunction. This is also the reasoning for some of the provided ranges of numbers. One of the [changes](https://github.com/microsoft/TypeScript/pull/44997) for TypeScript 4.6.1 will allow to change this limit.
# Usage
```ts
import { Range } from "ts-dependent-types";const logNumberInRange = (value: Range<0, 10>) => console.log(value);
``````ts
type TwoDigitHexadecimal = `${HexadecimalDigit}${HexadecimalDigit}`; // "00" | "01" | .. | "FF"const x64AssemblyCallInstruction: TwoDigitHexadecimal = "FF";
```# Exports
## Numbers
### `Increment`
`Increment` takes a number in the range of -24 to 24 and produces a literal of the following number
```ts
type Five = Increment<4>; // 5
```### `IncrementUnsigned`
`IncrementUnsigned` takes a number in the range of 0 to 48 and produces a literal of the following number
```ts
type FourtyEight = IncrementUnsigned<47>; // 48
```### `Decrement`
`Decrement` takes a number in the range of -24 to 24 and produces a literal of the previous number
```ts
type Three = Decrement<4>; // 3
```### `DecrementUnsigned`
`DecrementUnsigned` takes a number in the range of 0 to 48 and produces a literal of the previous number
```ts
type FourtySix = Decrement<47>; // 46
```### `Digit`
A type corresponding to the range of `0` to `9`
### `Range`
Represents a union of number literals between two numbers, supports `-24` to `24`
```ts
const logNumberInRange = (value: Range<0, 10>) => console.log(value);logNumberInRange(9);
logNumberInRange(10);
logNumberInRange(11); // this will error
type SupportedRange = Range<-24, 24>; // -24 | -23 | ... | 24
type UnsupportedRange = Range<0, 100>; // this will error
```### `RangeUnsigned`
Represents a union of number literals between two numbers, supports `0` to `48`
```ts
const logNumberInRange = (value: RangeUnsigned<30, 48>) => console.log(value);logNumberInRange(35);
logNumberInRange(40);
logNumberInRange(11); // this will error
type SupportedRange = RangeUnsigned<0, 48>; // 0 | 1 | ... | 48
type UnsupportedRange = RangeUnsigned<0, 100>; // this will error
```### `NextPowerOf2`
Represents 2^(T+1) (up to 2^48)
```ts
type SomePowerOf2 = NextPowerOf2<4>; // 8
```### `PowerOf2Range`
Represents a union of powers of 2 up to 2^T (0 < T < 49)
```ts
type FourPowersOf2 = PowerOf2Range<4>; // 2 | 4 | 8 | 16type SupportedPowersOf2 = PowerOf2Range; // 2 | 4 | ... | 281474976710656
```## Strings
### `Character`
A type corresponding to any printable ASCII character
### `HexadecimalDigit`
```ts
type TwoDigitHexadecimal = `${HexadecimalDigit}${HexadecimalDigit}`; // "00" | "01" | .. | "FF"
```### `UppercaseLetter`
Any uppercase ASCII letter
### `LowercaseLetter`
Any lowercase ASCII letter
### `Letter`
Any ASCII letter
### `SpecialCharacter`
Any printable special ASCII character
### `NextUppercaseLetter`
`Increment` takes a `Letter` and produces the follwing letter
```ts
type B = NextUppercaseLetter<'A'>; // "B"
```### `PreviousUppercaseLetter`
`Increment` takes an `UppercaseLetter` and produces the previous letter
```ts
type A = PreviousUppercaseLetter<'B'>; // "A"
```### `NextLowercaseLetter`
`Increment` takes a `LowercaseLetter` and produces the previous letter
```ts
type LowercaseB = NextLowercaseLetter<'a'>; // "b"
```### `PreviousLowercaseLetter`
`Increment` takes an `UppercaseLetter` and produces the previous letter
```ts
type LowercaseA = PreviousLowercaseLetter<'b'>; // "a"
```### `UppercaseLetterRange`
Produces a range of `UppercaseLetter`s between two given `UppercaseLetter`s
```ts
type AtoD = UppercaseLetterRange<'A', 'D'>; // "A" | "B" | "C" | "D"
```### `LowercaseLetterRange`
Produces a range of `LowercaseLetter`s between two given `LowercaseLetter`s
```ts
type LowercaseAtoD = LowercaseLetterRange<'a', 'd'>; // "a" | "b" | "c" | "d"
```# Prior Art
(TODO: add links and examples)