vincjo / datatables

A toolkit for creating datatable components with Svelte
https://vincjo.fr/datatables
MIT License
372 stars 15 forks source link

(Draft) Add generic types #33

Closed jst-r closed 1 year ago

jst-r commented 1 year ago

This PR adds generic type parameters to the datatables svelte library.

Changes are explained in some level of detail in the issue #31. I feel that picture below illustrates the idea pretty well. image showing type errors

Probably the easiest way to get a feel for the new feature is to clone the fork, npm install, and npm run dev. The datatables tab maps to the src/routes/examples. There you can see the autocomplete and static checks in action.

src/lib/core/Context.ts is a good starting point to look at the implementation as it references almost everything else. It also contains some general type definitions, which isn't ideal, but probably fine for now.

I haven't updated the documentation yet, but I'll try to do it today.

vincjo commented 1 year ago

No worries about the documentation site, there is no rush and I can take care of it gradually if you don't have time.

I was wondering if it was possible - later - to export generic T type from DataHandler ? (maybe it's not relevant); So that we can simplify the composition of the showcase components

From:

<script lang="ts">
     type T = $$Generic<{ [key: string]: unknown }>
     import type { DtatHandler } from '$lib/core'
    export let handler: DataHandler<T>
</script>

to:

<script lang="ts">
      import type { DtatHandler, T } from '$lib/core'
      export let handler: DataHandler<T>
</script>
jst-r commented 1 year ago

I'm not aware of any ways to do it like that. Howerer, we can create an instance of handler on the module level, and import in in the example.

jst-r commented 1 year ago

Like

// index.ts or something else
export const handler = new DataHandler(myData);

// Component.svelte
<script lang="ts">
import { handler } from "./index"
vincjo commented 1 year ago

Ok i get it thx. That's fine as it is then, the type declaration inside components make it more explicit

vincjo commented 1 year ago

I don't get why you don't appear as a contributor. Did I miss a step?