Reputation: 7899
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"
}
}
Upvotes: 32
Views: 113129
Reputation: 664
Like many other Vue/TS issues, this one was solved for me by Restarting the TypeScript Language Server in Visual Studio Code either from the command palette, or by closing/restarting VSCode.
Upvotes: -1
Reputation: 2960
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 :) 😎
Upvotes: 93
Reputation: 316
Create Folder d in src then create a file vue-treeselect.ts
and write declare module 'vue-treeselect';
in file vue-treeselect.ts
Upvotes: 0
Reputation: 5980
Because any of given solutions haven't helped me per se, I had to create a dedicated vendor.d.ts
(shims-vue.d.ts
did not work) file in my project root folder, and then put there following line:
declare module 'my-module-name-here';
Upvotes: 18
Reputation: 4936
If you are using vue, let's add as below to shims-vue.d.ts
file
declare module 'module-name-here' {
import { ModuleNameHere } from 'module-name-here'
export { ModuleNameHere }
}
Upvotes: 1
Reputation: 7899
It turns out the question was wrong, since I wanted to use '@riophae/vue-treeselect' but I was targeting 'vue-treeselect' instead.
But this was just one part of the problem, it is still not trivial (at least for me) to import 3rd party npm components into a Vue.js typescript project.
This is how I solved it:
Quick & dirty solution (when one does not care about the type and just wants to import the library)
Steps:
Change tsconfig.json
(the Typescript configuration of your project) to allow import Treeselect from '@riophae/vue-treeselect';
:
"noImplicitAny": false
(without this step you get an error that states: TS7016: Could not find a declaration file for module '@riophae/vue-treeselect'. 'path/to/project/node_modules/@riophae/vue-treeselect/dist/vue-treeselect.min.js' implicitly has an 'any' type.
)
Import module and register as 'treeselect':
import Treeselect from '@riophae/vue-treeselect';
Vue.component('treeselect', Treeselect);
(without this step you get an error that states: Unknown custom element: <treeselect> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
)
Use it in the view:
<treeselect v-model="value" :multiple="true" :options="source" />
Rember to set the style at the bottom of the .vue.html page:
<style src="@riophae/vue-treeselect/dist/vue-treeselect.min.css"></style>
If you want to do things properly, I highly recommend this article:
"Write a declaration file for a third-party NPM module"
Upvotes: 4
Reputation: 5199
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
}
Upvotes: 27