Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'fromNow' does not exist on type 'Dayjs' #297

Closed
yashwp opened this issue Aug 7, 2018 · 32 comments · Fixed by #418
Closed

Property 'fromNow' does not exist on type 'Dayjs' #297

yashwp opened this issue Aug 7, 2018 · 32 comments · Fixed by #418
Labels

Comments

@yashwp
Copy link

yashwp commented Aug 7, 2018

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

@dmk1111
Copy link

dmk1111 commented Aug 14, 2018

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

@iamkun
Copy link
Owner

iamkun commented Aug 14, 2018

@dmk1111 thanks for the reply.

Just wondering if we should add plugin methods like fromNow into our main d.ts file, or separated definition file.

@dmk1111
Copy link

dmk1111 commented Aug 14, 2018

@iamkun I suppose it better to keep them separate and than just import all of them to main file

@yashwp
Copy link
Author

yashwp commented Aug 14, 2018

@iamkun Can you tell me, when you are gonna release the new patch version with this issue fix?

@yashwp
Copy link
Author

yashwp commented Sep 6, 2018

@iamkun when can u fix this problem? Its almost a month now since I've created the issue?

@dmk1111
Copy link

dmk1111 commented Sep 6, 2018

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

@yashwp
Copy link
Author

yashwp commented Sep 10, 2018

@dmk1111 Bro I don't know how to work with....d.ts file. Otherwise, I would have done that...on the 2nd day itself... If you know can you help me with that?

@dmk1111
Copy link

dmk1111 commented Sep 10, 2018

@yashwp in case you want to know how to create *.d.ts files, you could refer to this SO thread:
https://stackoverflow.com/questions/18301898/generating-typescript-declaration-files-from-javascript

If you didn't manage to create it by yourself, I'll try to do this later, maybe in a week or so.

@iamkun
Copy link
Owner

iamkun commented Sep 18, 2018

This might help

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

@yashwp
Copy link
Author

yashwp commented Sep 25, 2018

@dmk1111 I tried to create .d.ts file but didn't get it. So can make it for me, that would be great help.

Thank u :)

@yashwp
Copy link
Author

yashwp commented Oct 10, 2018

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

@ypresto
Copy link
Contributor

ypresto commented Nov 29, 2018

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' {
    ...

@rdehuyss
Copy link

rdehuyss commented Feb 5, 2019

Also having issues with this... default installation of plugins does not work as described.

@timohofmeijer
Copy link

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
  }
}

@iamkun
Copy link
Owner

iamkun commented Feb 24, 2019

🎉 This issue has been resolved in version 1.8.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@wottpal
Copy link

wottpal commented Feb 24, 2019

Hey @iamkun and @ypresto, thanks for your work!

On 1.8.7 I'm getting the following error (ERROR in node_modules/dayjs/plugin/relativeTime.d.ts(1,22): error TS2305: Module '.../node_modules/dayjs/index"' has no exported member 'DateType'.) when doing the following imports:

import * as dayjs from 'dayjs'
import * as relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/de'

I guess this could be related to this issue, right?

@iamkun
Copy link
Owner

iamkun commented Feb 25, 2019

@wottpal Thanks for the feedback, that's my mistake.

Fixed in v1.8.8. 😬

@stunaz
Copy link

stunaz commented Mar 7, 2019

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 ....

@iamkun
Copy link
Owner

iamkun commented Mar 8, 2019

@stunaz dayjs version, please? And if in a TypeScript project, you could try import * as dayjs from 'dayjs'

@stunaz
Copy link

stunaz commented Mar 8, 2019

v 1.8.9 see for yourself

@iamkun
Copy link
Owner

iamkun commented Mar 8, 2019

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

@stunaz
Copy link

stunaz commented Mar 8, 2019

indeed @iamkun ... thanks!

@break7533
Copy link

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

@ko-devHong
Copy link

ko-devHong commented Oct 1, 2021

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;
    }
}

@designbyadrian
Copy link

Still having this issue with version 1.11.0. This old post from 2019 solved it.

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
  }
}

@iamkun
Copy link
Owner

iamkun commented Mar 26, 2022

@designbyadrian there should be a type file shipped with 1.11.0

@Kavignon
Copy link

What does that mean? I'm having a similar issue with this. I don't have much experience with frontend and simply explaining what I need to do would be great. I'm currently using the temporary fix and sitting on version 1.11.0

@huy-lv
Copy link

huy-lv commented May 18, 2022

try this:

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

@omerg
Copy link

omerg commented Aug 2, 2022

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)

@nima
Copy link

nima commented Mar 29, 2023

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);

@gajus
Copy link

gajus commented Sep 30, 2023

We had a weird issue where the function was not available depending on how it was re-exported. This appears to be minification/tree-shaking related. We are using Rollup. This fixed it.

import originalDayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat.js';
import calendar from 'dayjs/plugin/calendar.js';
import duration from 'dayjs/plugin/duration.js';
import isBetween from 'dayjs/plugin/isBetween.js';
import isToday from 'dayjs/plugin/isToday.js';
import isYesterday from 'dayjs/plugin/isYesterday.js';
import relativeTime from 'dayjs/plugin/relativeTime.js';

originalDayjs.extend(advancedFormat);
originalDayjs.extend(relativeTime);
originalDayjs.extend(duration);
originalDayjs.extend(calendar);
originalDayjs.extend(isYesterday);
originalDayjs.extend(isToday);
originalDayjs.extend(isBetween);

- export { default as dayjs } from 'dayjs';
+ export { originalDayjs as dayjs };

This only started happening after switching form commonjs to esm.

@alexey-sh
Copy link

alexey-sh commented Nov 11, 2023

dayjs version = 1.11.7
node version = v20.9.0
typescript = 4.9.5

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

dayjs.extend(relativeTime);

dayjs(date).locale(locale).fromNow(); // throws

throws TypeError: (0 , dayjs_1.default)(...).locale(...).fromNow is not a function error

in js:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });

const dayjs_1 = __importDefault(require("dayjs"));
const relativeTime_1 = __importDefault(require("dayjs/plugin/relativeTime"));
dayjs_1.default.extend(relativeTime_1.default);

(0, dayjs_1.default)(date).locale(locale).fromNow();

Two days later...

dayjs(date).locale(null).fromNow(); // throws
dayjs(date).locale('').fromNow(); // also throws

So I just changed

ago(date: Date, locale = 'en') {
  return dayjs(date).locale(locale).fromNow(); // throws
}
// to this
ago(date: Date, locale: string) {
  return dayjs(date).locale(locale || 'en').fromNow(); // throws
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.