lit-analyzer
lit-analyzer copied to clipboard
[lit-plugin] no-incompatible-type-binding with generics?
Getting error when property is using generics (types should match):
(property) .definition: T | undefined
Type 'StringFieldDefinition | undefined' is not assignable to 'T | undefined'lit-plugin(no-incompatible-type-binding)(2304)
Have to cast to any to get rid of the error (which is not very nice)
Related to #149
I see a similar problem when using the latest version of <vaadin-grid>
which has generics for declaring the grid item type.
Here's an example binding:
html`
<vaadin-grid
...
.dragFilter="${(model: GridItemModel<Person>) => {
const item = model.item;
return !item.manager;
}}"
>
...
</vaadin-grid>
`;
This highlights dragFilter
in red with the message:
Type '(model: GridItemModel
) => boolean' is not assignable to '<TItem>(model: GridItemModel<TItem>) => boolean | null | undefined'lit-plugin(no-incompatible-type-binding)(2304)
Here you can find the sources for latest vaadin-grid
. Here's the type declaration for dragFilter
https://github.com/vaadin/web-components/blob/master/packages/vaadin-grid/src/vaadin-grid-drag-and-drop-mixin.d.ts#L43
Here's a simpler case and some more context (if it helps): https://github.com/vaadin/docs/blob/635fd058f5d1cafa699038f54d619cd82c5e3ce7/frontend/demo/component/grid/grid-single-selection-mode.ts#L35
private selectedItems: Person[] = [];
...
html`
<vaadin-grid
...
.selectedItems="${this.selectedItems}"
>
...
</vaadin-grid>
`;
Shows:
Type 'default[]' is not assignable to '(TItem | null)[] | null'lit-plugin(no-incompatible-type-binding)(2304)
Screenshot:
Here is the type definition for selectedItems
:
interface SelectionMixin<TItem> {
selectedItems: Array<TItem | null> | null;
VS Code detects the type of <vaadin-grid>
element correctly as GridElement (via an HTMLElementTagNameMap)
And if I have a variable with the type GridElement
it is detected as GridElement<TItem = any>
and I have no problem imperatively setting grid.selectedItems = this.selectedItems;
(where it detects the type of the property as (property) SelectionMixin<any>.selectedItems: any[] | null
)
Would it be reasonable to think that lit-plugin should be able to handle this better? Imo it should detect that TItem
defaults to any
and not complain. Or would it be possible to somehow explicitly hint the item type in this kind of property binding?
crossposting https://github.com/runem/lit-analyzer/issues/149#issuecomment-1006162839