React Moment problem on DatePicker when using moment (Maybe dependencies)

Viewed 1011

I'm using DatePicker and I can't set the default Values, I tried this:

I import the moment like this:

import moment from 'moment';

//And then: 

<DatePicker 
   defaultValue={moment('2015/01/01', 'YYYY/MM/DD')}
/>

//Also tried this

<DatePicker 
   defaultValue={moment('2015/01/01')}
/>

But I got this error: "Type 'import("C:/git/my_project/node_modules/moment/moment").Moment' is not assignable to type 'moment.Moment'. Types of property 'add' are incompatible."

I can't find where is my error

When I type npm ls moment and npm ls @types/moment. This is the result:

https://i.stack.imgur.com/ZXfXr.png

1 Answers

In my case, I had multiple conflicting moment version. I ran npm ls moment and I got this:

├─┬ antd@4.2.2
│ ├── moment@2.25.3 
│ └─┬ rc-picker@1.4.16
│   └── moment@2.24.0  deduped
└── moment@2.24.0 

I also had @types/moment installed on my repository but moment now supports TypeScript (You can see a deprecation warning on the npm package page)

There are the steps I followed:

npm un --s @types/moment
npm i --s moment@2.25.3

Install whatever version antd is dependent on to remove the errors

Related