Although I am relatively new to strong typing, I have been immersed in TypeScript for a bit. To enhance my skills in strong typing and gain a better grasp of the type system in TypeScript, I've decided to tackle the Type Challenges.
One thing that has caught my attention is the distinction between two options for declaring a type:
type TupleOptions = Array<number | string>;
type TupleOptions = (number | string)[];
My initial thought was that they should essentially be the same. However, as I delve into Question 11 - Tuple to Object, I noticed differences when attempting to implement them.
// this works
type TupleToObject<T extends readonly (number | string)[]> = {
[key in T[number]]: key;
}
// this does NOT work
type TupleToObject<T extends readonly Array<number | string>> = {
[key in T[number]]: key;
}
Given my limited experience, this situation confuses me greatly.
Furthermore, apart from this issue, I am intrigued by why using `any[]` (generates errors) and `(number | string)[]` (successful) yield different outcomes for the final test in the challenge:
// @ts-expect-error
type error = TupleToObject<[[1, 2], {}]>
You can access the TypeScript Playground directly from here.