Lets assume we want to have type representing two elements array in typescript.
Its simpe
type twoElementsArray = number[] & { length : 2};
Now lets try same trick with Set
type twoElementsSet = Set & {size : 2}
const setOfTwo: twoElementsSet = new Set([1,2])
Now we have an error Type 'number' is not assignable to type '2'
Which looks a bit awkward. What is also worth noting while
trick with array works here
const arrayOfTwo : twoElementsArray = [1,2]
It doesn’t here
const anotherArrayOfTwo: twoElementsArray = new Array(1,2);
Do you have idea how one can enforce consistent behaviour so no matter how Array is constructed it can control number of elements on type level as well as Array/Set/Map whatever data structure symmetry ?