Skip to content

How to properly use UCarousel API with TypeScript #3053

@vircoding

Description

@vircoding

Description

I just tried the example about autoplay in UCarousel, but with TypeScript.

<script setup lang="ts">
const props = defineProps<{
  images: string[];
}>();

const carousel = useTemplateRef('carouselGallery');

onMounted(() => {
  setInterval(() => {
    if (!carousel.value) return;

    if (carousel.value.page === carousel.value.pages) {
      return carousel.value.select(0);
    }

    carousel.value.next();
  }, 5000);
});
</script>

<template>
  <UCarousel
    ref="carouselGallery"
    v-slot="{ item }"
    :items="props.images"
    :ui="{
      item: 'basis-full',
      indicators: { wrapper: 'bg-black/70 w-min px-2.5 py-1.5 gap-1 left-4 rounded-xl' },
    }"
    class="overflow-hidden"
    indicators
  >
    <img :src="item" class="w-full" draggable="true" />
  </UCarousel>
</template>

And I get some TypeScript errors:

Property 'page' does not exist on type 'CreateComponentPublicInstanceWithMixins(...)
Property 'select' does not exist on type 'CreateComponentPublicInstanceWithMixins(...)
Property 'next' does not exist on type 'CreateComponentPublicInstanceWithMixins(...)

How should I correctly infer the type for UCarousel?

Nuxt v3.15.1
NuxtUI v2.20.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions