-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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] Improve TS definition of unstyled #26642
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/material-ui-unstyled/scripts/testModuleAugmentation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const childProcess = require('child_process'); | ||
const { chunk } = require('lodash'); | ||
const glob = require('fast-glob'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
|
||
const exec = promisify(childProcess.exec); | ||
const packageRoot = path.resolve(__dirname, '../'); | ||
|
||
async function test(tsconfigPath) { | ||
try { | ||
await exec(['yarn', 'tsc', '--project', tsconfigPath].join(' '), { cwd: packageRoot }); | ||
} catch (error) { | ||
if (error.stdout !== undefined) { | ||
// `exec` error | ||
throw new Error(`exit code ${error.code}: ${error.stdout}`); | ||
} | ||
// Unknown error | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Tests various module augmentation scenarios. | ||
* We can't run them with a single `tsc` run since these apply globally. | ||
* Running them all would mean they're not isolated. | ||
* Each test case represents a section in our docs. | ||
* | ||
* We're not using mocha since mocha is used for runtime tests. | ||
* This script also allows us to test in parallel which we can't do with our mocha tests. | ||
*/ | ||
async function main() { | ||
const tsconfigPaths = await glob('test/typescript/moduleAugmentation/*.tsconfig.json', { | ||
absolute: true, | ||
cwd: packageRoot, | ||
}); | ||
// Need to process in chunks or we might run out-of-memory | ||
// approximate yarn lerna --concurrency 7 | ||
const tsconfigPathsChunks = chunk(tsconfigPaths, 7); | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for await (const tsconfigPathsChunk of tsconfigPathsChunks) { | ||
await Promise.all( | ||
tsconfigPathsChunk.map(async (tsconfigPath) => { | ||
await test(tsconfigPath).then( | ||
() => { | ||
// eslint-disable-next-line no-console -- test runner feedback | ||
console.log(`PASS ${path.relative(process.cwd(), tsconfigPath)}`); | ||
}, | ||
(error) => { | ||
// don't bail but log the error | ||
console.error(`FAIL ${path.relative(process.cwd(), tsconfigPath)}\n ${error}`); | ||
// and mark the test as failed | ||
process.exitCode = 1; | ||
}, | ||
); | ||
}), | ||
); | ||
} | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/material-ui-unstyled/test/typescript/moduleAugmentation/sliderStyleProps.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import * as React from 'react'; | ||
import { SliderUnstyled } from '@material-ui/unstyled'; | ||
|
||
declare module '@material-ui/unstyled' { | ||
interface SliderStylePropsOverrides { | ||
color?: 'primary' | 'secondary'; | ||
} | ||
} | ||
|
||
<SliderUnstyled componentsProps={{ root: { styleProps: { color: 'primary' } } }} />; | ||
|
||
// @ts-expect-error unknown color | ||
<SliderUnstyled componentsProps={{ root: { styleProps: { color: 'inherit' } } }} />; |
4 changes: 4 additions & 0 deletions
4
...es/material-ui-unstyled/test/typescript/moduleAugmentation/sliderStyleProps.tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../../../../tsconfig", | ||
"files": ["sliderStyleProps.spec.tsx"] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@mnajdova Are we sure about these types? I'm lost when I look at them. In MUI-X @dtassone has used
any
which might be too loose, but seems still better.
I mean, for instance, if I want to add an extra
data-
attribute it fails:On
styleProps
specifically, I also don't see why we useSliderUnstyledTypeMap
. As far as I know, the correlation between the props and what's available in this prop is "light". For instance, in the slider, we have an extramarked
property:https://github.com/mui-org/material-ui/blob/d36858d36741bb57facb7f1e91d0d2d4d760b4bb/packages/material-ui-unstyled/src/SliderUnstyled/SliderUnstyled.js#L624
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.
I'm in favor of using specific types instead of
any
whenever possible. This way developers get prop validation for free from Typescript (this can be especially important if a component with mandatory props is used in a slot).The
data-foo
you mentioned is a bit of an edge case as data attributes are not specified in anyHTMLAttributes
. To use them in a type-safe way, module augmentation should be used. However, IMO this should be the only case where developers should resort to module augmentation when providingcomponentsProps
.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.
@michaldudak
data-*
,tabindex
,aria-*
, they all fail. We don't accept any other props thanas
andstyleProps
. The issue for me is that we don't have any easy way to know what are types accepted. A developer could usecomponents
to render something completely different.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.
In this case yes, they'll fail without module augmentation. But we could use what I made in https://github.com/mui-org/material-ui/blob/d587b1eab467739a2a143f0cd49bfc10cc35ba74/packages/material-ui-unstyled/src/SwitchUnstyled/SwitchUnstyled.tsx - when you pass in a component/intrinsic element in
components
prop, the correspondingcomponentsProps
expects props for this exact component/element.data-*
attributes obviously aren't supported this way, but all the others are (includingaria-*
)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.
@michaldudak This approach seems to have potential 👍 .
Should we open a new issue to keep track of this problem?
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.
Sure thing: #26810