How do I write a requirement for a specific item in a typescript array? Let's consider for example "number array with atleast one number seven". It seems that requiring a specific element to be seven is easy but requiring "one of the elements" is more difficult.
I tried the following. However, it produces Type alias 'ContainsSeven' circularly references itself. ts(2456)
error.
type ContainsSeven =
|[7, ...Array<number>]
|[number, ...ContainsSeven]
const example1: ContainsSeven = [1,2,7]
const example2: ContainsSeven = [1,7,2]
const example3: ContainsSeven = [7,1,2]
// @ts-expect-error
const counter1: ContainsSeven = []
// @ts-expect-error
const counter2: ContainsSeven = [1,2,3]
My ultimate goal is to use standard JavaScript Arrays instead of a custom linked structure to implement lazy concatenation of generators. See related diff for a more comprehensive example.