-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c949496
commit 5b2cba9
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { type Add } from './math'; | ||
|
||
export type D<T extends number> = number extends T ? number :_D<T, []>; | ||
type _D<T extends number, R extends number[]> = R['length'] extends T ? | ||
R[number] : | ||
_D<T, [...R, Add<R['length'], 1>]>; | ||
|
||
export interface Dice<T extends number> { | ||
roll(): D<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type Add<N extends number, AmountToAdd extends number> = _Add<N, AmountToAdd, [], []>; | ||
type _Add< | ||
N extends number, | ||
AmountToAdd extends number, | ||
LeftAmount extends unknown[], | ||
RightAmount extends unknown[] | ||
> = | ||
LeftAmount['length'] extends N ? | ||
RightAmount['length'] extends AmountToAdd ? | ||
[...LeftAmount, ...RightAmount]['length'] : | ||
_Add<N, AmountToAdd, LeftAmount, [unknown, ...RightAmount]> : | ||
_Add<N, AmountToAdd, [unknown, ...LeftAmount], []> | ||
|
||
export type Sub<N extends number, AmountToSub extends number> = _Sub<N, AmountToSub, [], [], []>; | ||
type _Sub< | ||
N extends number, | ||
AmountToSub extends number, | ||
Take extends unknown[], | ||
Left extends unknown[], | ||
All extends unknown[], | ||
> = | ||
All['length'] extends N ? | ||
Left['length'] : | ||
Take['length'] extends AmountToSub ? | ||
_Sub<N, AmountToSub, Take, [unknown, ...Left], [unknown, ...All]>: | ||
_Sub<N, AmountToSub, [unknown, ...Take], [], [unknown, ...All]>; |