Designing a TypeScript declaration file for a Vue library (Vue DateTime)

Currently, I am utilizing Vue.js along with Typescript for my project. My intention is to incorporate the vue-datetime package. However, since this package lacks a Typescript definition file, I have taken the initiative to create a basic version myself.

To accomplish this, I have inserted an index.d.ts file into the node_modules/vue-datetime directory:

import Vue from 'vue';

declare class Datetime extends Vue { }
declare class DatetimePopup extends Vue { }

export default Datetime;
export { Datetime, DatetimePopup };

I can now import and use the package in my Vue component like this (please note that I am employing the vue-class-component package, hence the slightly different syntax).

<template>
    <div>
        <v-datetimepicker v-model="someproperty" class="myclass"></v-datetimepicker>
    </div>
</template>

<script lang="ts">

    import Vue from 'vue';
    import Component from 'vue-class-component';
    import { Datetime } from 'vue-datetime';

    import 'vue-datetime/dist/vue-datetime.css';

    Vue.component('v-datetimepicker', Datetime);

    @Component({
        components: { Datetime }
    })
    export default class MyComponent extends Vue {

    }
</script>

<style>
</style>

The setup works perfectly fine. However, I do not wish to store my node_modules folder in source control. To address this, I decided to relocate the definition file to the same directory as my Vue component (are there better alternatives?). I moved the index.d.ts file, renaming it to datepicker.d.ts. Subsequently, I updated my Vue component to import the datepicker like so:

import { Datetime } from './datepicker'
. Although this does not trigger any compile errors, upon loading the page, I encounter the Javascript error:
Uncaught Error: Cannot find module './datepicker'
.

In my quest to resolve this issue, I have explored various avenues online without success. Do you have any suggestions on what I could attempt or where the problem might lie?

Answer №1

Latest Update

A new typed definition file has recently been included for the vue-datetime package within the DefinitelyTyped repository.

Initial Solution

I managed to successfully resolve the issue by following a few steps:

1. I altered the name of the declaration file from datepicker.d.ts to MyComponent.d.ts. By ensuring that 'MyComponent' matched the exact Vue component name where the package was utilized, and storing the declaration file in the same directory as the component.

2. The content of the declaration file was modified to:

declare module 'vue-datetime' {
    import Vue from 'vue';
    class Datetime extends Vue { }
    class DatetimePopup extends Vue { }
    export { Datetime, DatetimePopup }
}

3. Returning the import line back to

import { Datetime } from 'vue-datetime';
instead of
import { Datetime } from './datepicker'
.

Following these adjustments, everything now compiles without errors during runtime.

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

Tips for adjusting the day layout (M to Mo, F to Fr, etc.) in Vuetify's date and month pickers

I am attempting to modify the format of days in the Date/month picker component. Currently, the list of days is displayed as "M,T,W,T,F,S,S", but I need it to be "Mo,Tu,We,Th,Fr,Sa,Su". In the API documentation for Vuetify, I noticed a parameter called "da ...

Having trouble with the "Vs Code nx console generate" command? It seems that there are no flags available to configure

My issue involves the nx console extension installed in my Visual Studio Code. Every time I attempt to use the generate command for components, services, or libraries, I receive an error message stating "ng generate @schematics/angular:component This com ...

Encountered an issue in Angular 2 when the property 'then' was not found on type 'Subscription'

I have been attempting to call a service from my login.ts file but I am encountering various errors. Here is the code snippet in question: login.ts import { Component } from '@angular/core'; import { Auth, User } from '@ionic/cloud-angular ...

The loop displays only the final value stored in the array

I'm currently facing an issue where I am trying to display a set of values from an array, but it only seems to be showing the last value. How can I ensure that all values are displayed? When I inspect longLat[itemIndex], I notice that the values shou ...

Adding up elements in a vector using TypeScript

Is there a way to calculate the sum of values based on the name without repetition in "dataGrf"? I've tried various methods but am facing difficulties. Here is a brief example below to help illustrate what I'm attempting to achieve. Note: Please ...

Guide to Integrating BLK Theme into an Angular CLI Project

I've recently set up an Angular project using CLI and I am interested in integrating the BLK theme by Creative Tim into this project. However, the only available option from Creative Tim is to download a pre-existing project and build upon that framew ...

Angular firing off select option with object properties

Within my Angular application, I am faced with a situation involving a <select> element that contains a list of <option> elements whose values are associated with objects. My goal is to capture the last selected value using the following code: ...

Obtain the current date from the data source to utilize in a Vuetify component

I am looking to incorporate a vuetify calendar that highlights today's date. How can I retrieve the current date and assign it as a variable in the data? Below is an example of calendar code from the Vuetify webpage for reference. This particular exa ...

A Guide on Effectively Implementing v-model in VueJS with Child Components

Hey there! I'm just diving into VueJS and currently wrapping my head around how components function. Recently, I encountered a puzzling situation while attempting to modify a data parameter of a parent that had been passed to a child component used as ...

The distinction between <Type>function and function name():Type in TypeScript is essential to understand the various ways

function retrieveCounter(): Counter { let counter = <Counter>function (start: number) { }; counter.interval = 123; return counter; } Upon inspection of line 2 in the code snippet above, I am left wondering why I am unable to use function ...

Encountered an error while trying to load config.ts file because of an issue

Trying to set up a new protractor project to conduct tests on an angular site. Node.js, typescript, protractor, and jasmine are all installed globally. After running webdriver-manager update and webdriver-manager start in the project folder, I proceed to b ...

How can one define a getter within an interface?

One of my classes is structured like this (only showing a portion here): export class LinkedListNode<t> extends windward.WrObject implements ILinkedListNode<t> { public get next(): LinkedListNode<t> { return this._next === thi ...

Issue encountered when working with interface and Observable during the http parsing process

Visual Studio File Structure Error app(folder) -->employee-list(folder) -->employee-list.component.html -->employee-list.component.ts -->app.component.html -->app.component.ts -->app.module.ts -->employee.json ...

What is the reason for my algorithm's inability to work with this specific number?

I'm currently working on creating an algorithm to compute the sum of prime numbers that are less than or equal to a specified number. Below is my attempt: function calculatePrimeSum(num) { // initialize an array with numbers up to the given num let ...

Running into an issue while attempting to update a Firebase array using the .where() method. The error message states that ".update isn't

I'm currently in the process of updating a document in my Firebase that contains an array. At this point, a user's document might appear as follows: username: John postedProjects: ['project-one', 'project-two'] As John subm ...

The offline functionality of the Angular Progressive Web App(PWA) is experiencing difficulties

As per the official guidelines, I attempted to create a PWA that functions in offline mode using pure PWA without angular-cli. However, despite following the official instructions, I was unable to make it work offline. The document in question can be foun ...

bespoke numerical values within the Ionic 2 spectrum

The usual ionic range has a set minimum and maximum value for the possible range of values. <ion-range min="1" max="20" ...> However, I am in need of a custom range where the slider should only allow these specific values: {1, 5, 7, 20}. Is there ...

Using React as a monorepo with yarn workspaces, typescript, and leveraging absolute imports

Struggling to configure a React project using yarn workspaces and typescript, with the following folder structure: -root -package.json -workspaces -web -common Here is the content of my package.json file: { "name": "my-project-name", "private" ...

The category of properties cannot be assigned to the data type 'string'

Within my codebase, there exists a component known as StepperTile. This component has the ability to accept two specific props: title_part_1 title_part_2 However, when attempting to render this component, I encountered an error within the StepperTile fil ...

Issues arising from using async/await in conjunction with .then() in vue.js login function, causing fetch process not to wait for completion

I implemented a login function to verify credentials on my backend server, but I am facing an issue with waiting for the server response. Despite following the es7-async-await.js guide and trying various async/await and promise techniques, the function sti ...