vue-moment icon indicating copy to clipboard operation
vue-moment copied to clipboard

Typescript support

Open adamalfredsson opened this issue 6 years ago • 10 comments

I'm not sure how I would import the Moment type through this package. Would I need to also include the momentjs dependency for types in my project?

adamalfredsson avatar Oct 01 '18 11:10 adamalfredsson

Either

  • use the js syntax Vue.use(require('vue-moment')); or
  • leverage the typescript default exports:
import VueMoment from 'vue-moment';
Vue.use(VueMoment);

takahser avatar Oct 16 '18 10:10 takahser

I'm still not able to get the types working:

in main.ts:

import moment from 'vue-moment';
Vue.use(moment);

in MyComponent.vue:

screen shot 2018-10-16 at 13 12 02

adamalfredsson avatar Oct 16 '18 11:10 adamalfredsson

I haven't done that much in Typescript before, does anyone have any experience with this that can point me in the right direction?

BrockReece avatar Oct 16 '18 12:10 BrockReece

@nomadoda you can't use intellisense yet, since there are not typings for this project. You can, however, still use it together with typescript.

@BrockReece you need to provide some typescript typings for that. Maybe this helps: https://www.triplet.fi/blog/three-ways-to-provide-typescript-type-definitions-to-3rd-party-libraries/

takahser avatar Oct 16 '18 13:10 takahser

https://github.com/brockpetrie/vue-moment/pull/111

Rouche avatar Nov 27 '18 21:11 Rouche

This is kind of old but it's still open. I'm actually here for something else but I worked around lack of @types package by creating vue-moment.d.ts in a /types directory. It contains the following:

import Vue from 'vue'

declare module 'vue/types/vue' {
  interface Vue {
    $moment: any
  }
}

And then in main.ts

Vue.use(moment as any)

jayporta avatar May 01 '19 22:05 jayporta

I fixed typings by creating a d.ts file with the following contents:

import Vue, { VueConstructor } from 'vue';
import moment from 'moment';

declare module 'vue/types/vue' {
  interface Vue {
    $moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment;
    $moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment;
  }
  interface VueConstructor {
    $moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment;
    $moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment;
  }
}

demianh avatar Oct 16 '19 15:10 demianh

Based on @demianh script, I made some changes, because it doesn't work using this.$moment, only work calling method this.$moment(). I needed because I use moment-timer extension.

With below vue.d.ts changes it is possible to use like this lines:

this.$moment
     .duration(5, "minutes")
     .timer({ start: true }, callback);

const tokensExpiry = this.$moment().add(data.expires_in, 's').toISOString();
// @types/vue.d.ts
import * as moment from "moment";

interface ITimerAttr {
  wait?: number;
  loop?: boolean;
  start?: boolean;
}

interface Callback {
  (): void;
}

interface Timer {
  start(): boolean;
  stop(): boolean;
  clearTime(): boolean;
  updateStartEndTickFromDuration(duration: number): boolean;
  getDuration(): number;
  getRemainingDuration(): number;
  isStopped(): boolean;
  isStarted(): boolean;
}

interface IMoment extends moment.Moment, Timer {
  (
    inp?: moment.MomentInput,
    format?: moment.MomentFormatSpecification,
    language?: string,
    strict?: boolean
  ): IMoment;
  duration(
    inp?: moment.DurationInputArg1,
    unit?: moment.DurationInputArg2
  ): IMoment;

  timer(attributes: ITimerAttr, callback?: Callback): IMoment;
}

declare module "vue/types/vue" {
  interface Vue {
    $moment: IMoment;
  }
  interface VueConstructor {
    $moment: IMoment;
  }
}

alvaro-canepa avatar Mar 30 '20 11:03 alvaro-canepa

I think this works too given that the moment dependent package already contains extensive typings.

Type declarations:

import Vue from 'vue'
import moment from 'moment';

declare module 'vue/types/vue' {
    interface Vue {
        $moment: typeof moment
    }
}

lifenautjoe avatar Apr 23 '20 12:04 lifenautjoe

Perfect! Thanks guys!

cafe01 avatar Apr 25 '20 11:04 cafe01