-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[typescript] Better type annotations for unstyled components
and componentsProps
props
#26810
Comments
A reproduction related to this issue: https://codesandbox.io/s/basicalerts-material-demo-forked-pqs4e?file=/demo.tsx. |
components
and componentsProps
propscomponents
and componentsProps
props
We discussed the topic with @eps1lon and he pointed out that in some cases this may not work as expected and can create more problems then it can solve. I'm therefore abandoning the effort and will leave componentsProps untyped. |
@michaldudak OK sounds great. Did you check the codebase to make sure all the components follow the same approach? Bases on #26810 (comment), it's not clear to me that we do. I suspect we need a follow up Or maybe even better. What's the current standard/recommendation regarding this problem? So that @mui-org/x can also follow the convention. |
The SliderUnstyled mentioned in #26810 (comment) as well as other unstyled components (except for the Switch) use (slighly) typed |
@michaldudak I had a look at what we have in the codebase, it seems that we could normalize: componentsIt seems to be about
/**
* The set of overridable components used in the grid.
*/
components: {
/**
* The custom Checkbox component used in the grid.
*/
Checkbox: React.ElementType;
/**
* Column menu component rendered by clicking on the 3 dots "kebab" icon in column headers.
*/
ColumnMenu: React.JSXElementConstructor<any>; componentsPropsIt seems to be about
/**
* Overrideable components props dynamically passed to the component at rendering.
*/
componentsProps?: {
checkbox?: any;
columnMenu?: any;
errorOverlay?: any;
footer?: any;
header?: any;
toolbar?: any;
preferencesPanel?: any;
loadingOverlay?: any;
noResultsOverlay?: any;
noRowsOverlay?: any;
pagination?: any;
filterPanel?: any;
panel?: any;
columnsPanel?: any;
} |
AFAIK you can't assign HTML element names to JSXElementConstructor, so ElementType seems to be a better option. As for componentsProps, we could at least constrain it to be an object with string keys, so |
@michaldudak True, so I guess it depends on either we envision the support for a React host element or not for the specific slot? I mean, as soon as we provide custom props, it's KO for using JSXElementConstructor.
|
I'm reopening so we can use this issue as a pretext to normalize. |
If there are cases where we require a custom component we could use ComponentType or JSXElementConstructor. Otherwise ElementType should work. |
@mnajdova You were using |
Yeah, let's go with it. Developers can anyway augment the props interface if they want to make it stricter. |
Summary 💡
Improve developer experience by providing type information in
componentsProps
when a component/element is passed in the respectivecomponents
field.Once a developer populates the
components.X
field, they will see intellisense hints incomponentsProps.x
with all the props available to pass to the X component.Examples 🌈
Preliminary work has been done in: michaldudak@a9f37ba
The problem with this solution is that it causes PropTypes to be generated incorrectly. For
components
fields, the expected prop type would bePropTypes.elementType
, but a union of all intrinsic element names andPropTypes.func
is generated. This is problematic when a class based component, or a one created withReact.forwardRef
is passed in.Motivation 🔦
Having detailed hints in intellisense and the ability to check types before runtime will lead to increased developer happiness and productivity. As an application developer myself, I value well-crafted type definitions that help during development.
The text was updated successfully, but these errors were encountered: