Using Typescript 3.8.3.
I'm trying to set up types for the following data, but cannot for the life of me figure it out.
type Info = {
title: string;
description: string;
items: Array<Item<object>>;
}
type Item<T> = {
title: string;
data: T;
}
const info: Info = {
title: 'some title',
description: 'some description',
items: [
{ title: 'title1', data: { param1: 'something', param2: 'something else' } },
{ title: 'title2', data: { param3: 'abc' } },
{ title: 'title3', data: { param1: 'not the same as above, just the key name', param4: 123 } }
]
};
info.items[0].data.param1 // Property 'param1' does not exist on type 'object'
I know Array<Item<object>>
is wrong, but I can't figure out how I can infer type of the union of an array of elements.
Is what I want to achieve even possible?
The only thing you can do is:
If
info
is not 'const' type, you can modify it like:info.items = []
, so typescript cannot guarantee the value / type ofinfo.items[0].data.param1