'typescript array of generic items [duplicate]

I have a situation where i defined a generic type

type ItemAndArray<T> = {
    item: T,
    array: T[]
}

i want to define another type as an array of items from this type

type ArrayOfItemAndArray = (ItemAndArray<any>)[]

but this definition doesn't really encapsulate my type restrictions well since this code compiles:

const val: ArrayOfItemAndArray = [
    {
        item: 'string',
        array: [1, 2, 3] // shouldn't compile since string and number are not the same type
    }
]

if i was writing in a language like java i would use something like

    public class ItemAndArray<T> {
        public T item;
        public T[] array;

        public ItemAndArray(T item, T[] array) {
            this.item = item;
            this.array = array;
        }
    }

and i would use wildcard generic to specify the array type

    ItemAndArray<?>[] val = {new ItemAndArray<String>("string", new Integer[]{1,2,3})}; // doesn't compile since Integer array should be string array

does anyone have an idea on how to specify this type?

thanks in advanced!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source