Skip to content

Generics

vue
<script lang="ts" setup generic="T extends object">
defineProps<{
  options: T[];
}>();

defineEmits<{
  (evt: 'change', val: T): void;
}>();
</script>
svelte
<script lang="ts" generics="T extends object">
  import { createEventDispatcher } from 'svelte';

  export let options = T[];

  const dispatch = createEventDispatcher<{ change: T }>();
</script>
svelte
<script lang="ts">
  import { createEventDispatcher } from 'svelte';

  type T = $$Generic<object>;
  export let options = T[];

  const dispatch = createEventDispatcher<{ change: T }>();
</script>
tsx
interface Props<T> {
  options?: T[];
  onChange?: (val: T) => void;
}

export function Select<T extends object>(props: Props<T>) {
  /* ... */
}

Released under the MIT License.