 # Typescript Caveat 2: keyof {[x:string]:string} is string | number

`type A = {[x:string]:string} type B = keyof A // string | number//   ^?const A:A = {a:"abc",:"xyz"}type C = keyof A & string // string//   ^?`

playground

normally this will not cause any issue, because number key in javascript will coerce into string key

however this…

--

--

# Typescript Numeric Literal Types Y / X (Division)

In the last post we know multiplication is possible

In this post we are going to try division

`type CreateArrayWithLengthX<    LENGTH extends number,    ACC extends unknown[] = [],> = ACC['length'] extends LENGTH    ? ACC    : CreateArrayWithLengthX<LENGTH, [...ACC,1]>type Division<Dividend extends number, Divisor extends number, ACC extends unknown[] = []…`

--

--

# Typescript Numeric Literal Types X-Y (Subtraction)

We have seen how can we add two numeric literals type

now can we apply similar tricks for subtraction?

yes we can, this is how we do it:

`type CreateArrayWithLengthX<    LENGTH extends number,    ACC extends unknown[] = [],> = ACC['length'] extends LENGTH…`

--

--

# Typescript Turn Tuple Type to Array

here is how you do it

`type TupleToArray<  T extends unknown[],  Acc = never> = T extends [infer H, ...infer Rest]  ? Rest extends []    ? (H | Acc)[]    : TupleToArray<Rest, Acc | H>  : "impossible route"type A = TupleToArray<[number, string, boolean]> // (string | number | boolean)[]`

--

--