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

feat: improve typing for DataTable row and cell #816

Closed

Conversation

KoichiKiyokawa
Copy link
Contributor

@KoichiKiyokawa KoichiKiyokawa commented Sep 13, 2021

fix #667 (maybe)

Thank you for such a nice library 🎉
When using DataTable component, it would be great if the editor autocompletes headers prop or slot values(cell.key or row).

I make it possible with Generics(named Row). For example,

export interface DataTableEmptyHeader<Row extends DataTableRow = DataTableRow>

extends DataTableRow restrict Row type (e.g. it should have id property). = DataTableRow is to avoid breaking change.

Feel free to:

  • Close this PR if not suitable.
  • Change my code.

without say anything.

demo (autocomplete is working correctly by this PR)

data-table-typing.mp4
code in demo
<script>
  import { DataTable } from "carbon-components-svelte";

  const rows = [
    { id: 1, name: "user1", age: 10, gender: "male" },
    { id: 2, name: "user2", age: 20, gender: "female" },
  ];
</script>

<DataTable
  rows="{rows}"
  headers="{[{ key: 'name', value: 'Name' }, { key: 'age', value: 'Age' }, { key: 'gender', value: 'Gender' }]}"
>
  <div slot="cell" let:cell let:row>
    {#if cell.key === 'age'}{row.age}(years old){:else}{cell.value}{/if}
  </div>
</DataTable>

@metonym metonym self-requested a review September 14, 2021 15:56
@metonym
Copy link
Collaborator

metonym commented Sep 17, 2021

Started testing locally – this looks very promising!

This may also address this issue: #364

@brunnerh
Copy link
Contributor

brunnerh commented Apr 20, 2022

There is a bit of wrinkle with key: keyof Row; in that keys can also access nested objects or not exist at all. But I would love to have generics for the slot properties, otherwise it requires a lot of type assertions when working in TypeScript.

REPL demonstrating other key uses

Using interpolated string literal types it it actually possible to get nested keys as well. The types may be a bit hard to read and it does not handle the synthetic keys case, though.

TS Playground example for nested key resolution

(This pull request does not fix #667, because that issue is about the runtime type.)

@metonym metonym force-pushed the master branch 2 times, most recently from e7485c4 to 417102d Compare November 8, 2023 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataTable runtime row type is lost
3 participants