Error in VueJS/Typescript: Module 'my-module' or its type declarations are not found

Hey fellow developers! I'm currently working on a Vue2 setup with Nuxt and Typescript. I'm facing an issue while trying to install the awesome vue-slick-carousel module via yarn. When attempting to import the module in my component, Typescript throws an error: "Cannot find module 'vue-slick-carousel' or its corresponding type declarations". As someone new to Typescript, I could really use some guidance!

<script lang="ts">
  import Vue from 'vue'
  import { VueSlickCarousel } from 'vue-slick-carousel'
 
  export default Vue.extend({
     components: {
       VueSlickCarousel
     }
     ...
  })
</script>

This is what I have in my tsconfig.json file:

{

"compilerOptions": {
    "target": "ES2018",
    "module": "ESNext",
    "moduleResolution": "Node",
    "lib": [
      "ESNext",
      "ESNext.AsyncIterable",
      "DOM"
    ],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ]
    },
    "types": [
      "@types/node",
      "@nuxt/types",
      "nuxt-i18n"
    ]
  },
  "exclude": [
    "node_modules",
    ".nuxt",
    "dist"
  ]
}

In addition, I have a vue-shim.d.ts file, but I'm unsure about what needs to be included there:

import Vue from 'vue'
declare module 'vue/types/vue' {
  interface Vue {
    $i18n: {
      locale:string,
      locales:{code:string, name:string, file:string}[]
    }
  }
}

The root cause seems to be Typescript's inability to locate the module types. I came across a library named "@types/slick-carousel" which provides the necessary types, but I'm unsure about the next steps. Do I need to manually define these types?

Answer №1

Unfortunately, the @types/slick-carousel package does not provide accurate types for this specific package. To verify this, you can examine its index.d.ts file.

To address this issue, you have a couple of options. One is to include // @ts-ignore above the problematic line, although this method is not highly recommended.
Alternatively, you can create a file named shims-slick-carousele.d.ts in the src folder with the following code:

declare module 'vue-slick-carousel' {
  import Vue from "vue";

  declare class Slick extends Vue {
      next(): void;
      prev(): void;
      play(): void;
      pause(): void;
      reSlick(): void;
      goTo(index: number): void;
  }

  export default Slick;
}

(The type definitions were sourced from here).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The import declaration is clashing with the local declaration of 'defineProps' in Vue version 3.3

Today, I encountered several errors after updating my node_modules and Vue to v3.3. The project utilizes Vue 3.3, WebPack (not Vite), and VS Code Volar. It is a large project. In every file with *.vue containing <script setup lang="ts">, ...

Retrieve the value of an object without relying on hardcoded index values in TypeScript

I am working with an object structure retrieved from an API response. I need to extract various attributes from the data, which is nested within another object. Can someone assist me in achieving this in a cleaner way without relying on hardcoded indices? ...

Creating a data structure that filters out specific classes when returning an object

Consider the following scenario: class MyClass {} class MyOtherClass { something!: number; } type HasClasses = { foo: MyClass; bar: string; doo: MyClass; coo: {x: string;}; boo: MyOtherClass; }; type RemovedClasses = RemoveClassTypes& ...

Transforming numbers into arrays in JavaScript/TypeScript for Angular 7

What is the best way to convert the number 10 into an array in JavaScript? expected output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] OR How can I transform the number 10 into the number of items within an array using JavaScript? ...

Error when using ES6 modules in ts-jest

I keep running into an 'unexpected token' error whenever I run my tests using ts-jest. To show you exactly what's going on, I've created a small repo with all of my current configurations available here: https://github.com/ramoneguru/t ...

The configuration of the property has not been declared (Error: <spyOnProperty>)

Imagine having a MenuComponent @Component({ selector: 'cg-menu', templateUrl: './menu.component.html', styleUrls: [ './menu.component.scss' ] }) export class MenuComponent implements OnInit { menu: MenuItem[]; isLog ...

Utilize Vue to call a method by providing its name as a string

When designing a navbar, I encountered the need to include a list of buttons. Some of these buttons should act as links, while others should call specific methods. For example, clicking on "Home" should direct the user to /home and clicking on "Log out" sh ...

VueJS advisory: Refrain from directly altering a prop

When attempting to modify a prop value using the @click directive, I encountered a warning message: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed pr ...

Utilizing numerous 'select' HTML elements with Vue.js and Materializecss

I've been working on creating an HTML multiple select form element with Vue.js and encountered an interesting issue. Everything was functioning perfectly following the guidance provided in this resource. However, when I decided to incorporate the Mate ...

Typescript's Confusion with Array Types: Understanding Conditional Types

Here is the setup I have. The concept is to receive a generic and options shape, deduce the necessary options based on the generic and the type key of the options shape, and appropriately restrict the options. type OptionProp<T extends string | boolean& ...

Is it a good idea to separate TypeScript types into their own package?

In my React/TypeScript application, I have approximately 100 files where various types are declared. I am looking for a simpler and more automated approach to extract all these types into a separate package. Is there a method other than manually copying ...

Maintain Angular Dropdown Menu Open Across Page Refresh

I am currently working on an HTML/Typescript view that is connected to a SQL Database. Whenever there are changes made to the database, the entire webpage reloads. The issue we are facing is that we have dropdown menus on the page that clients want to rema ...

Node.js does not allow the extension of the Promise object due to the absence of a base constructor with the required number of type

I'm trying to enhance the Promise object using this code snippet: class MyPromise extends Promise { constructor(executor) { super((resolve, reject) => { return executor(resolve, reject); }); } } But I keep encou ...

What is the contrast between element.getAttribute() value and a String in protractor?

When using protractor and typescript, I need to verify that the text saved in a textbox matches a certain string by comparing it with the resulting value of element.getAttribute("value"). Unfortunately, getText() does not work for this scenario b ...

Utilize a prefix when importing Vue from the node_modules directory

Setting up my development environment on Ubuntu 20.04 involves using the following tools: PhpStorm 2020.3 as my IDE CodeIgniter v4 Vue 3.x Tailwind CSS+UI After creating a test project, I followed the documentation and ran the following commands: npm ins ...

Trouble encountered while configuring and executing Electron combined with React, Typescript, and Webpack application

I am currently in the process of migrating my Electron application from ES6 to Typescript. Despite successfully building the dll and main configurations, I encounter a SyntaxError (Unexpected token ...) when attempting to run the application. The project c ...

Puzzle: Ensuring all properties of JavaScript objects within an array are defined

Consider the vue.js object provided below: FormattedData: Object 1: Object approved: true 2: Object approved: undefined 3: Object approved: false 4: Object approved: true Seeking a more effective and concis ...

Linking a Vue application within a PHP page using router hotlinking

Currently, I am in the process of developing a module for a CMS (Joomla) using VUE 3 with Router for the frontend. The prototype is functional and ready to be integrated into the CMS Module. The router is also working smoothly. However, when a user navigat ...

A guide on triggering a function in Angular 6 when scrolling up or down

Is there a way to trigger a function when a user scrolls up or down in an Angular 6 project? I want to implement a feature similar to fullpage.js in my Angular 6 application. I initially attempted to achieve this using Angular animations, but without succ ...

How to access the ID value from the URL within a different component that was passed as a prop in

I have a scenario where I am building a reusable component with two child components in the following flow: Child Component 1 -> Parent Component -> Super Parent Main Component In the child component, I define a prop called 'url' which is ...