Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find a declaration file for module 'vue-xxx'

Any 3rd party Vue.js library that I try to add to my project, throws the following error:

Could not find a declaration file for module 'vue-xxx'

I have tried 'vue-treeselect', 'vue-select', 'vue-multiselect' with more or less the same result (* those libraries don't allow import Modulename from, but need const Modulename = require).

I add those under "dependencies": { in package.json.

I am using the SPA template for Vue.js provided by dotnet core.


Full example with 'tree-select':

import Treeselect from 'vue-treeselect';

/* ... */

value = null;
source = [
    {
        id: 'node-1',
        label: 'Node 1',
        children: [
            {
                id: 'node-1-a',
                label: 'Node 1-A',
            }
      ]
    },
    {
        id: 'node-2',
        label: 'Node 2',
    }
  ];

/* ... */

<treeselect v-model="value"
                :multiple="true"
                :options="source" />

Error:

in [at-loader] ./ClientApp/components/mycomponent/mycomponent.ts:10:24 
    TS7016: Could not find a declaration file for module 'vue-treeselect'. 'path/to/project/node_modules/vue-treeselect/dist/vue-treeselect.js' implicitly has an 'any' type.

package.json

{
  "name": "MyProject",
  "private": true,
  "version": "0.0.0",
  "dependencies": {
    "dygraphs": "^2.1.0",
    "ol3-layerswitcher": "^1.1.2",
    "vue-treeselect": "^1.0.6"
  },
  "devDependencies": {
    "@types/dygraphs": "^1.1.6",
    "@types/webpack-env": "^1.13.0",
    "@types/vue": "^2.0.0",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "^3.0.0",
    "bootstrap": "^3.3.6",
    "css-loader": "^0.25.0",
    "event-source-polyfill": "^0.0.7",
    "extract-text-webpack-plugin": "^2.0.0-rc",
    "file-loader": "^0.9.0",
    "isomorphic-fetch": "^2.2.1",
    "jquery": "^3.1.1",
    "style-loader": "^0.13.1",
    "typescript": "^2.2.1",
    "url-loader": "^0.5.7",
    "vue": "^2.5.13",
    "vue-loader": "^14.2.1",
    "vue-property-decorator": "^6.0.0",
    "vue-router": "^3.0.1",
    "vue-template-compiler": "^2.5.13",
    "webpack": "^2.2.0",
    "webpack-hot-middleware": "^2.12.2"    
  }
}
like image 421
Xavier Peña Avatar asked Mar 13 '18 14:03

Xavier Peña


2 Answers

This error occured because of vue-xxxxxx has been generated in JavaScript.

Method 1: To avoid the issue I just replace it by the standard require():

const vue-xxxxxx = require('vue-xxxxxx');
// import { vue-xxxxxx } from 'vue-xxxxxx'

Method 2: To avoid the issue I just replace these line in the tsconfig.json
"noImplicitAny": false,
"allowJs": true,

Now you can use import statement as follows:
import { vue-xxxxxx } from 'vue-xxxxxx'

Enjoy :)

like image 156
Mohammad Ayoub Khan Avatar answered Oct 16 '22 00:10

Mohammad Ayoub Khan


If @types/vue-treeselect does not exist then you can manually declare the module using ambient definitions.

Create a .d.ts somewhere in your project (warning: it should not use import or export) and define your module. If you don't want to type-check it (makes everything imported to be infered as any) :

declare module 'vue-treeselect'

If you want to type-check it :

declare module 'vue-treeselect' {
  export function something(): void
}
like image 27
Fathy Avatar answered Oct 15 '22 23:10

Fathy