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
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
@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.
@iamkun I suppose it better to keep them separate and than just import all of them to main file
@iamkun Can you tell me, when you are gonna release the new patch version with this issue fix?
@iamkun when can u fix this problem? Its almost a month now since I've created the issue?
@yashwp you could introduce PR with desired fixes, in case you want those changes to appear faster
@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?
@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.
This might help
declare module 'dayjs' {
interface Dayjs {
fromNow();
}
}
@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 :)
@dmk1111 @iamkun Can u fix this thing anytime soon? It's been 2 months....
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' {
...
Also having issues with this... default installation of plugins does not work as described.
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
}
}
:tada: This issue has been resolved in version 1.8.8 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
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?
@wottpal Thanks for the feedback, that's my mistake.
Fixed in v1.8.8. 馃槵
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 ....
@stunaz dayjs version, please? And if in a TypeScript project, you could try import * as dayjs from 'dayjs'
v 1.8.9 see for yourself
@stunaz I see. You should use dayjs().from rather than dayjs.from according to our document.
indeed @iamkun ... thanks!
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
Most helpful comment
You need at least one import statement to use Module Augmentation.
Also it seems you can nest module augmentation in
declare module.