Typescript Numeric Literal Types X * Y (Multiplication)

type CreateArrayWithLengthX<
LENGTH extends number,
ACC extends unknown[] = [],
> = ACC['length'] extends LENGTH
? ACC
: CreateArrayWithLengthX<LENGTH, [...ACC,1]>
type Multiplication<X extends number, Y extends number, Z extends number[] = []> =
[...CreateArrayWithLengthX<Y>]['length'] extends Z['length']
?[]
:[...CreateArrayWithLengthX<X>, ...Multiplication<X,Y,[1,...Z]>]
type A = Multiplication<526,19>['length'] // 9994
type B = Multiplication<1,48>['length'] // error if 2nd interger exceed 47
type CreateArrayWithLengthX<
LENGTH extends number,
ACC extends unknown[] = [],
> = ACC['length'] extends LENGTH
? ACC
: CreateArrayWithLengthX<LENGTH, [...ACC,1]>
type Multiplication<X extends number, Y extends number, Z extends number[] = [], V extends unknown[] = []> =
[...CreateArrayWithLengthX<Y>]['length'] extends Z['length']
? V['length']
: Multiplication<X,Y,[1,...Z],[...CreateArrayWithLengthX<X>,...V]>
type A= Multiplication<19,526> // 9994
type B= Multiplication<526,19> // 9994

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Acid Coder

Acid Coder

Typescript Zombie. Youtube Pikachu On Acid.