Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript generics to allow declaring GridItem type #2104

Closed
Haprog opened this issue Dec 15, 2020 · 1 comment · Fixed by vaadin/web-components#209
Closed

TypeScript generics to allow declaring GridItem type #2104

Haprog opened this issue Dec 15, 2020 · 1 comment · Fixed by vaadin/web-components#209
Assignees
Labels
enhancement New feature or request typescript TypeScript definitions issue

Comments

@Haprog
Copy link
Contributor

Haprog commented Dec 15, 2020

Description

Currently the TS type declarations use type GridItem = unknown; for the item type in all types/interfaces. It would be great if the types would use generics where the item type could default to GridItem (unknown) but it would be able to be overridden in app code when declaring data providers or renderers for example, so that you don't need to explicitly cast the unknown to something else to be able to use item references with TS.

I think all types that use GridItem (directly or nested) should have generics for setting item type.

e.g.

export type GridDataProviderCallback<T = GridItem> = (
  items: Array<T>,
  size?: number
) => void;

export type GridDataProviderParams<T = GridItem> = {
  page: number;
  pageSize: number;
  filters: Array<GridFilter>;
  sortOrders: Array<GridSorter>;
  parentItem?: T;
};

export type GridDataProvider<T = GridItem> = (
  params: GridDataProviderParams<T>,
  callback: GridDataProviderCallback<T>
) => void;

Expected code for accessing props on custom item type

const dataProvider: GridDataProvider<MyItem> = (params, callback) => {
  const foo = params.parentItem.myItemProp;
  // ...
};

Actually needed code for accessing props on custom item type

const dataProvider: GridDataProvider = (params, callback) => {
  const foo = (params.parentItem as MyItem).myItemProp;
  // ...
};
@Haprog Haprog added enhancement New feature or request typescript TypeScript definitions issue labels Dec 15, 2020
@web-padawan web-padawan self-assigned this Jan 7, 2021
@Haprog
Copy link
Contributor Author

Haprog commented Mar 4, 2021

Also related #2071

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request typescript TypeScript definitions issue
Projects
None yet
3 participants