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)
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:This might help
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)
You need at least one import statement to use Module Augmentation.
Also it seems you can nest module augmentation in
declare module
.Similarly, if not in a module (from @omerg’s post), this worked for me:
@yashwp you could introduce PR with desired fixes, in case you want those changes to appear faster
method
fromNow
is not present ind.ts
file, that’s why TS throws error. You could add typeany
todayjs
orrelativeTime
and check if it works after that or you could createrealativeTime.d.ts
file for that plugin and make PR@stunaz I see. You should use
dayjs().from
rather thandayjs.from
according to our document.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
try this:
fixed, merged, released… 💯 How to use it now? 🔢 I am trying this :
error :
Property 'from' does not exist on type ....
@dmk1111 @iamkun Can u fix this thing anytime soon? It’s been 2 months…