carbon-design-system / sveld

Generate TypeScript definitions for your Svelte components
https://sveld.onrender.com
Apache License 2.0
404 stars 21 forks source link

feat(component-parser): support generics #127

Closed metonym closed 4 months ago

metonym commented 4 months ago

Currently, to define generics for a Svelte component, you must use generics attribute on the script tag. Note that this feature is experimental and may change in the future.

However, the generics attribute only works if using lang="ts"; the language server will produce an error if generics is used without specifying lang="ts".

<!-- This causes an error because `lang="ts"` must be used. -->
<script generics="Row extends DataTableRow = any"></script>

Because sveld is designed to support JavaScript-only usage as a baseline, the API design to specify generics uses a custom JSDoc tag @generics.

Signature:

/**
 * @generics {GenericParameter} GenericName
 */

Example

/**
 * @generics {Row extends DataTableRow = any} Row
 */

The generated TypeScript definition will resemble the following:

export default class Component<Row extends DataTableRow = any> extends SvelteComponentTyped<
  ComponentProps<Row>,
  Record<string, any>,
  Record<string, any>
> {}