When listItems contain the size prop, like the following:
const listItems = [
{
label: 'First link',
href: '#/display/list-group',
iconType: 'calendar',
size: 's',
},
]
return <EuiListGroup listItems={listItems} />
TypesScript complains:
Types of property 'size' are incompatible.
Type 'string' is not assignable to type '"s" | "m" | "xs" | "l" | undefined'.
The group size prop works just fine though:
<EuiListGroup listItems={listItems} size={'s'} />
This is an unfortunate aspect of TypeScript, where your listItems variable is detected as the type:
listItems: {
label: string;
href: string;
iconType: string;
size: string;
}[]
And size's actual value is no longer known. A couple options to get around this:
as const, so TS treats the value as 's' instead of the genric stringconst listItems = [
{
label: 'First link',
href: '#/display/list-group',
iconType: 'calendar',
size: 's' as const,
},
]
EuiListGroupItemProps[], forcing TS to validate at creation.import { EuiListGroupItemProps } from '@elastic/eui'
const listItems: EuiListGroupItemProps[] = [
{
label: 'First link',
href: '#/display/list-group',
iconType: 'calendar',
size: 's',
},
]
<EuiListGroup
listItems={[
{
label: 'First link',
href: '#/display/list-group',
iconType: 'calendar',
size: 's',
},
]}
/>
awesome, thanks for the detailed explanation @chandlerprall.
Most helpful comment
This is an unfortunate aspect of TypeScript, where your
listItemsvariable is detected as the type:And
size's actual value is no longer known. A couple options to get around this:as const, so TS treats the value as's'instead of the genricstringEuiListGroupItemProps[], forcing TS to validate at creation.