Skip to content

Instantly share code, notes, and snippets.

@mhdcodes
Created December 20, 2023 10:45
Show Gist options
  • Select an option

  • Save mhdcodes/ef0f12aeddcde12a8df04cefc5957166 to your computer and use it in GitHub Desktop.

Select an option

Save mhdcodes/ef0f12aeddcde12a8df04cefc5957166 to your computer and use it in GitHub Desktop.
Typescript
import type { Subtract, Compare } from 'ts-arithmetic';
import type { Equal, Expect } from '@type-challenges/utils'
type Modulo<N1 extends number, Bound extends number> = Compare<Bound, N1> extends 0
? 0
: Compare<Bound, N1> extends 1 ? N1 : Subtract<N1, Bound>
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<Modulo<0, 3>, 0>>,
Expect<Equal<Modulo<1, 3>, 1>>,
Expect<Equal<Modulo<2, 3>, 2>>,
Expect<Equal<Modulo<3, 3>, 0>>,
Expect<Equal<Modulo<4, 3>, 1>>,
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment