dayjs: Property 'fromNow' does not exist on type 'Dayjs'

I’m using the relativeTime plugin in my .ts file

Importing is done in below manner : - import * as dayjs from 'dayjs'; import relativeTime from 'dayjs/plugin/relativeTime'; dayjs.extend(relativeTime);

I’ve also tried to change the import statement as: import dayjs from 'dayjs'; But started throwing new error as no exported member

Now when I’m using in the fromNow function as: - dayjs(this.created_at).fromNow(); the compiler is throwing an error

Property 'fromNow' does not exist on type 'Dayjs' I’m using "dayjs": "^1.7.4", version

What’s the with usage? How can I fix this?

@iamkun Please address this issue

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 32 (8 by maintainers)

Most upvoted comments

import * as dayjs from 'dayjs';
import * as relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

This worked fine for me, in case anyone still needs it

Changing the way relativeTime is imported should allow getting access to corresponding type definitions:

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)

This might help

declare module 'dayjs' {
    interface Dayjs {
        fromNow();
    }
}

So a temporary fix, until plugin typings are merged, is adding the following to your typings: (took me some fiddling, so perhaps it can save someone else the trouble)

// From: https://github.com/iamkun/dayjs/pull/418/files#diff-e5e546dd2eb0351f813d63d1b39dbc48

import { Dayjs, PluginFunc } from 'dayjs'
type DateType = string | number | Date | Dayjs

declare module 'dayjs' {
  interface Dayjs {
    fromNow(withoutSuffix?: boolean): string
    from(compared: DateType, withoutSuffix?: boolean): string
    toNow(withoutSuffix?: boolean): string
    to(compared: DateType, withoutSuffix?: boolean): string
  }
}

You need at least one import statement to use Module Augmentation.

import 'dayjs'
declare module 'dayjs' {
    interface Dayjs {
      ...

Also it seems you can nest module augmentation in declare module.

declare module 'dayjs/plugin/relativeTime' {
  module 'dayjs' {
    ...

Similarly, if not in a module (from @omerg’s post), this worked for me:

const dayjs = require('dayjs');
const relativeTime = require('dayjs/plugin/relativeTime');
dayjs.extend(relativeTime);

@yashwp you could introduce PR with desired fixes, in case you want those changes to appear faster

method fromNow is not present in d.ts file, that’s why TS throws error. You could add type any to dayjs or relativeTime and check if it works after that or you could create realativeTime.d.ts file for that plugin and make PR

@stunaz I see. You should use dayjs().from rather than dayjs.from according to our document.

import * as dayjs from 'dayjs';
import * as relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

This worked fine for me, in case anyone still needs it

The problem was solved using the above answer, but the type error still occurs, so the type was declared to eliminate it.

/@types/dayjs/index.d.ts

import { Dayjs, PluginFunc } from "dayjs";

declare module "dayjs" {
    interface Dayjs {
        fromNow(withoutSuffix?: boolean): string;
    }
}

try this:

import dayjs from 'dayjs';
const relativeTime = require('dayjs/plugin/relativeTime');
dayjs.extend(relativeTime);

fixed, merged, released… 💯 How to use it now? 🔢 I am trying this :

import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';

 dayjs.extend(relativeTime);
 console.log(dayjs.from(dayjs('1990')));

error : Property 'from' does not exist on type ....

@dmk1111 @iamkun Can u fix this thing anytime soon? It’s been 2 months…