Comments (10)
It was working well before with MUI X v6, without this fix.
In v6, the type of the date was inferred from the props;
This mean that TypeScript was able to complain for the following code:
// Invalid because `value` and `onChange` should have the same typing
<LocalizationProvider dateAdapter={AdapterDayjs]>
<DatePicker value={new Date()} onChange={(date: Dayjs) => {}} />
</LocalizationProvider>
But it was not able to complain for the following code:
// `value` and `onChange` are coherent, but they are not compatible with `AdapterDayjs`
<LocalizationProvider dateAdapter={AdapterDayjs]>
<DatePicker value={new Date()} onChange={(date: Date) => {}} />
</LocalizationProvider>
This was super problematic because people tend to pass Date
objects when using adapters like AdapterDayjs
and complain that it does not work.
Our solution was to limit the valid types for the date to the one supported by the adapter(s) you imported.
In your example, you are using AdapterDateFns
, so the valid date is Date
.
It seems that you are importing it outside of the typescript project used to check the file that throws an error.
Without more information it is hard to know why, this is probably related to how you application is structure and TS configured (and it can be configured that way for very good reasons).
You should be able to get rid of the error by adding the AdapterDateFns
to the typescript project.
I see 3 solutions to do it depending on your setup and what you prefer:
- Add an
import type {} from '@mui/x-date-pickers/AdapterDateFns'
to a file at the root of your application (_app
onNext.js
for example) - Add
"./node_modules/@mui/x-date-pickers/AdapterDateFns/index.d.ts"
to yourtsconfig.json
file (this only works if you did not exclude all thenode_modules
, which is the case onNext.js
and maybe in other frameworks...). - Create a file
importAdapter.ts
somewhere, addimport type {} from '@mui/x-date-pickers/AdapterDateFns'
to it add register it intsconfig.json
(it's a mix of 1. and 2. if you don't have a good file to put it in).
@LukasTy I do agree that a section in the migration guide would be a nice addition
from mui-x.
I'm also seeing the same issue.
With MUI X v7 and date-fns
TS2344: Type Date does not satisfy the constraint never
Here is the code of my custom component, extending the default DatePicker
:
export interface CustomDatePickerProps<
TDate extends PickerValidDate,
TEnableAccessibleFieldDOMStructure extends boolean = false,
> extends DatePickerProps<TDate, TEnableAccessibleFieldDOMStructure> {
// custom properties here...
}
function CustomDatePicker<TDate extends PickerValidDate, TEnableAccessibleFieldDOMStructure extends boolean = false>(
props: CustomDatePickerProps<TDate, TEnableAccessibleFieldDOMStructure>,
) {
// simplified version of my code (in real life I use styled to customize the style of DatePicker)
return <DatePicker {...props} />
}
Usage:
<CustomDatePicker<Date> // --> TS2344: Type Date does not satisfy the constraint never
value={new Date()}
/>
Edit: declaring the specific module (cf. @LukasTy reply below) fix the issue. But I'm not sure to understand why. My React app is already using LocalizationProvider
with the AdapterDateFns
.
It was working well before with MUI X v6, without this fix.
from mui-x.
Thank you for your help and replies.
The issue was on my side due to a wird dependencies issues between the app consuming the design system and the design system itself.
Are you building a component library?
Yes
Do you plan to have multiple date libraries/adapters used?
Yes
from mui-x.
Thanks @LukasTy . I am building a component library and using tsup. Its in the build process this fails (Storybook works fine). Your explanation makes perfect sense. Storybook does initialize a LocalizationProvider with the AdapterDateFnsV3 adapter. As a component library, we leave the downstream app to be responsible for initializing the LocalizationProvider. Thanks for your help!
from mui-x.
Our component library is for internal use only so we've standardized on date-fns. Thanks for the tips, may be a better approach to main flexibility.
from mui-x.
@guicara I tried reproducing your problem, but wasn't able to trigger the mentioned error if AdapterDateFns
or AdapterDateFnsV3
is imported anywhere in the app. 🤷
Could you provide a minimal reproduction case (a GH repo or StackBlitz demo) for us to explore?
A minor nitpick regarding your wrapped component types:
Are you building a component library?
Do you plan to have multiple date libraries/adapters used?
Do you plan to use Pickers with both the regular input as well as the accessible DOM structure?
If not, consider statically defining your wrapped component interface:
export interface CustomDatePickerProps extends DatePickerProps<Date, false> {
// custom properties here...
}
It was working well before with MUI X v6, without this fix.
In v7 we have introduced more type safety, because prior to v7 you could use AdapterDayjs
, but could have been passing Date
props to your components, which would have caused issues. 🙈
We haven't flagged this change as a BC and haven't added a mention in the migration guide, but maybe we should have done (do) that?
cc @flaviendelangle
from mui-x.
Hello @andylacko, I was unable to reproduce your issue.
Please provide a minimal reproduction example. A StackBlitz or a minimal repository would be best.
Thank you. 🙏
from mui-x.
I'm also seeing the same issue. I simply took the Joy UI sample code for the date range picker (JoyUI Sample Code). I changed the date provider to be date-fns and used pro version (@mui/x-date-pickers-pro).
As I was trying to create a standalone component I removed the wrapper components for JoyV6MultiInputRangeField as this is done elsewhere.
I then removed the unused imports and the issue started appearing. By leaving the following unused import in the code, the issue goes away. If you remove it, the issue appears
import { AdapterDateFns } from '@mui/x-date-pickers-pro/AdapterDateFnsV3';
from mui-x.
I then removed the unused imports and the issue started appearing. By leaving the following unused import in the code, the issue goes away. If you remove it, the issue appears
@CWThomson Which adapter and where are you using in your application?
Importing an adapter anywhere in your application will augment the declared module with relevant interface entry and make TS happy with the provided Date
type.
TL/DR: I don't see a problem here, everything seems to work as expected. 👌
Are you by any chance building a component library and there is no place where a LocalizationProvider
with the appropriate dateAdapter
is defined? 🤔
from mui-x.
@CWThomson does your library only support Date
(date-fns) only?
Otherwise, it would probably make sense to keep the generics on your side as well. 🤔
If you specifically need TS to work with specific types, without using an adapter, maybe you could just declare the specific module yourself? 🤔
mui-x/packages/x-date-pickers/src/AdapterDateFnsBase/AdapterDateFnsBase.ts
Lines 121 to 125 in b7848b4
from mui-x.
Related Issues (20)
- [DateTimeRangePicker] Different selection flow option HOT 2
- [data grid] Clipboard copy places unnecessary quotations when copied text contains a quotation HOT 4
- [data grid] Export to Excel causes rows to be misaligned from their headers (with checkboxes present) HOT 3
- [data grid] rendering complex components in a cell push pieces to following rows HOT 3
- [data grid] Support the custom header filter height that is independent from the column header height HOT 9
- [fields] Can not type into date picker after dismissing calendar picker HOT 3
- [TreeView] Improve keyboard navigation based on the ARIA spec
- The GridValueFormatterParams interface has NOT been removed in v7 HOT 1
- [DataGrid] Server-side filter demo not working HOT 1
- [DatePicker] DatePicker dayjs date format not working v7.0.0 HOT 1
- [data grid] Module '"@mui/x-data-grid-premium"' has no exported member 'GridColDef'. HOT 3
- [pickers] Allow to customize the size / density of the views HOT 5
- [fields][DateField] fails to accept '22' as day keybord input in FR locale with format Do MMM YYYY HOT 2
- [question] After updating data grid to version 7, cells do not handle overflow and render ellipsis by default HOT 8
- [data grid] Nested grouping and sort by aggregation does not sort the second level HOT 6
- [charts] Rendering a Bar Chart with an empty dataset throws an error HOT 3
- [pickers] V7 LocalizationProvider not accepting AdapterMoment HOT 5
- [charts] Add Maps support
- [pickers] Move internal localization hook `useLocaleText` to Public API HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mui-x.