As a newcomer to Vue, I am eager to dive into creating components and publishing packages to NPM. My plan is to develop Vue (typescript) + Vuetify reusable components that can be easily installed from NPM into any of my projects. While I have successfully imported my HelloWorld component into a vue.js project, I encountered an error when trying to do the same in a Vue ts project:
'Could not find a declaration file for module 'sastify'. '/home/raphael/tester-ts/node_modules/sastify-nv/dist/sastify.common.js' implicitly has an 'any' type.
Try npm install @types/sastify-nv
if it exists or add a new declaration (.d.ts) file containing declare module 'sastify-nv';
I'm wondering how to generate this .d.ts file that would work similarly to my JS project.
Here is the structure of my project:
── babel.config.js
├── package.json
├── postcss.config.js
├── README.md
├── src
│ ├── components
│ │ └── HelloWorld
│ │ └── HelloWorld.vue
│ └── sastify.js
├── tsconfig.json
├── tslint.json
└── yarn.lock
The content of my package.json file:
{
"name": "sastify-nv",
"version": "0.1.6",
"private": false,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --name sastify ./src/sastify.js",
"lint": "vue-cli-service lint --fix",
"build:ts": "tsc"
},
"files": [
"dist",
"src"
],
"main": "dist/sastify.common.js",
"dependencies": {
"vue": "^2.6.10",
"vue-property-decorator": "^8.1.0",
"vuetify": "^2.0.0"
},
"devDependencies": {
"@vue/cli-plugin-typescript": "^3.11.0",
"@vue/cli-service": "^3.11.0",
"sass": "^1.17.4",
"sass-loader": "^7.1.0",
"typescript": "^3.4.3",
"vue-cli-plugin-vuetify": "^0.6.3",
"vue-template-compiler": "^2.6.10",
"vuetify-loader": "^1.2.2"
}
}
The content of my tsconfig.json file:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"declaration": true,
"outDir": "lib",
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
"vuetify"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
The content of my sastify.js file:
export { default as HelloWorld } from './components/HelloWorld/HelloWorld.vue'
The content of my HelloWorld.vue file located at components/HelloWorld/HelloWorld.vue:
<template>
<v-card>Vcard from HelloWorld</v-card>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { VCard } from "vuetify/lib";
@Component({
name: "HelloWorld",
components: {
VCard
}
})
export default class HelloWorld extends Vue {}
</script>
<style scoped>
</style>