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 · 22 comments

Comments

Projects
None yet
8 participants
@yashwp
Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner

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

This comment has been minimized.

Copy link

commented Aug 14, 2018

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

@yashwp

This comment has been minimized.

Copy link
Author

commented Aug 14, 2018

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

@yashwp

This comment has been minimized.

Copy link
Author

commented Sep 6, 2018

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

@dmk1111

This comment has been minimized.

Copy link

commented Sep 6, 2018

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

@yashwp

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner

commented Sep 18, 2018

This might help

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

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Author

commented Oct 10, 2018

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

@ypresto

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Feb 5, 2019

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

@timohofmeijer

This comment has been minimized.

Copy link

commented Feb 15, 2019

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 iamkun closed this in #418 Feb 23, 2019

@iamkun

This comment has been minimized.

Copy link
Owner

commented Feb 24, 2019

🎉 This issue has been resolved in version 1.8.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun added the released label Feb 24, 2019

@wottpal

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner

commented Feb 25, 2019

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

Fixed in v1.8.8. 😬

@stunaz

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner

commented Mar 8, 2019

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

@stunaz

This comment has been minimized.

Copy link

commented Mar 8, 2019

@iamkun

This comment has been minimized.

Copy link
Owner

commented Mar 8, 2019

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

@stunaz

This comment has been minimized.

Copy link

commented Mar 8, 2019

indeed @iamkun ... thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.