-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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
[Slider][material] Fix type dependency on @types/prop-types #37853
Conversation
Netlify deploy previewhttps://deploy-preview-37853--material-ui.netlify.app/ Bundle size report |
I don't think that these component types should depend on prop-types in the first place, this looks very strange to me: https://unpkg.com/browse/@mui/material@5.13.7/Slider/SliderValueLabel.d.ts. I also suspect that this slows TypeScript down. So I would propose we remove the dependency in the first place: diff --git a/packages/mui-material/src/Slider/SliderValueLabel.tsx b/packages/mui-material/src/Slider/SliderValueLabel.tsx
index d5f1364b87..fef5077057 100644
--- a/packages/mui-material/src/Slider/SliderValueLabel.tsx
+++ b/packages/mui-material/src/Slider/SliderValueLabel.tsx
@@ -49,4 +49,4 @@ SliderValueLabel.propTypes = {
children: PropTypes.element.isRequired,
className: PropTypes.string,
value: PropTypes.node,
-};
+} as any; Same for Material UI v6 cc @DiegoAndai diff --git a/packages/mui-material-next/src/Slider/SliderValueLabel.tsx b/packages/mui-material-next/src/Slider/SliderValueLabel.tsx
index d5f1364b87..fef5077057 100644
--- a/packages/mui-material-next/src/Slider/SliderValueLabel.tsx
+++ b/packages/mui-material-next/src/Slider/SliderValueLabel.tsx
@@ -49,4 +49,4 @@ SliderValueLabel.propTypes = {
children: PropTypes.element.isRequired,
className: PropTypes.string,
value: PropTypes.node,
-};
+} as any; Per
It's the only case in diff --git a/scripts/buildTypes.mjs b/scripts/buildTypes.mjs
index d8d3c1977f..66495ecf56 100644
--- a/scripts/buildTypes.mjs
+++ b/scripts/buildTypes.mjs
@@ -54,6 +54,22 @@ async function main() {
async function rewriteImportPaths(declarationFile) {
const code = await fse.readFile(declarationFile, { encoding: 'utf8' });
+ const basename = path.basename(declarationFile);
+
+ if (
+ // Only consider React components
+ basename[0] === basename[0].toUpperCase() &&
+ code.indexOf("import PropTypes from 'prop-types';") !== -1
+ ) {
+ throw new Error(
+ [
+ `${declarationFile} imports from 'prop-types', this is wrong.`,
+ "It's likely missing a cast to any on the propTypes declaration:",
+ 'ComponentName.propTypes = { /* prop */ } as any;',
+ ].join('\n'),
+ );
+ }
+
let fixedCode = code;
const changes = []; as well, and fix |
I have updated the PR to do an |
@oliviertassinari Let me know if there's anything else I can do. I can make the changes to |
Thanks for the contribution @Methuselah96! Also I think we should implement the changes @oliviertassinari proposed to We don't need to do it for v6 Slider, with the restructure PR: #37644 (just merged), the |
a516acb
to
e672805
Compare
@DiegoAndai Sounds good. I've updated the usages of
Any ideas on how to resolve this? |
I assume this is because the
and so some type is probably detecting the |
Alright, this is a little janky, but I changed |
The failing build is complaining about |
There's something I don't understand, are the Ripple and SliderValue files the only ones causing the issue? We use proptypes for all components, why aren't there other files causing issues? About the |
@DiegoAndai Yes, those are the only components causing issues. It looks like the other components aren't causing issues because either:
|
9f565c2
to
b123fbc
Compare
b123fbc
to
7ae7941
Compare
@Methuselah96 It would indeed make more sense to me, this diff --git a/packages/typescript-to-proptypes/src/generator.ts b/packages/typescript-to-proptypes/src/generator.ts
index face64ba54..de046a8619 100644
--- a/packages/typescript-to-proptypes/src/generator.ts
+++ b/packages/typescript-to-proptypes/src/generator.ts
@@ -334,8 +334,11 @@ export function generate(component: t.Component, options: GenerateOptions = {}):
const propTypesMemberTrailingComment = ensureBabelPluginTransformReactRemovePropTypesIntegration
? '/* remove-proptypes */'
: '';
- const propTypesCasting = disablePropTypesTypeChecking ? ' as any' : '';
const propTypesBanner = comment !== undefined ? comment : '';
- return `${component.name}.propTypes ${propTypesMemberTrailingComment}= {\n${propTypesBanner}${generated}\n}${propTypesCasting}`;
+ if (disablePropTypesTypeChecking) {
+ return `(${component.name} as any).propTypes ${propTypesMemberTrailingComment}= {\n${propTypesBanner}${generated}\n}`;
+ }
+
+ return `${component.name}.propTypes ${propTypesMemberTrailingComment}= {\n${propTypesBanner}${generated}\n}`;
} However, it would be way too many changes for this PR, let's stick to the current convention to keep the PR small in scope.
Rebasing on HEAD seems to fix it, unless it's removing the dead
@DiegoAndai Most of the files for Material UI v5 are in JavaScript, we have been manually authoring the TypeScript definitions (.d.ts) precisely to solve this class of issues we solve with this PR. Now, the types of the Slider in Material UI v6 look a bit broken:
|
This looks ready to merge to me, let's wait and see if @oliviertassinari has anything else to add
I will review these separately, no need to fix those here, thanks for pointing those out. |
Co-authored-by: Nathan Bierema <nbierema@gmail.com> Signed-off-by: Diego Andai <diego@mui.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes are ready to merge 🚀
@Methuselah96 have you tested the fix after all the changes? is it fixing the original issue you had?
The changes in the built declaration files look correct and should fix the issue. I haven't got the chance to actually try it in my app, but I'm confident it will fix the original issue. |
There is a type error when using a strict package manager because
SliderValueLabel.d.ts
imports from@types/prop-types
, but@mui/material
has no dependency on@types/prop-types
:This PR fixes this error by adding a dependency on
@types/prop-types
.