runem / lit-analyzer

Monorepository for tools that analyze lit-html templates
MIT License
318 stars 36 forks source link

[lit-plugin] no-incompatible-type-binding with generics? #163

Open dflorey opened 3 years ago

dflorey commented 3 years ago

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)

a11delavar commented 3 years ago

Related to #149

Haprog commented 3 years ago

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 '(model: GridItemModel) => 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

Haprog commented 3 years ago

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:

image

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?

mohe2015 commented 2 years ago

crossposting https://github.com/runem/lit-analyzer/issues/149#issuecomment-1006162839