-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Typescript typings breaking when omitting props for TextField #19579
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
Comments
Thanks for opening this issue and filling out the template. This is in essence a duplicate of #15697. More explanation and possible workaround can be found in #15697 (comment). |
Hi there, thanks for the reply and link to the other issue. While the issue defiantly appears in the same way I think there is a bit of subtle difference in the two issues. For instance in my case I am using the Omit<> type to specifically omit a unrelated key and it was the Omit<> process that was mutating the type into the manner seen in the other issue. This was one of the reasons for it confusing me so much as I was not interacting with the variant attribute that was giving the error and I somewhat naively believed Omit<> to be perfect. Therefor I refocused my bug / issue hunting on to typescript itself and found this issue - microsoft/TypeScript#31501 In that issue the different implementations of Omit<> and their subtle differences are discussed with proposed other implementations. Using the proposed OmitBetter<> and OmitBetterStrict<> both fixed my issue. Hope this can help anyone else in the future <3 |
I am offering this comment to help anyone who comes across this issue, as I have today. I found my way here because I was converting a component to TypeScript that wraps a Here's a simplified reproduction of the issue I was experiencing: // TypeScript error on `TextField`
const MyTextFieldWrapper: React.FC<TextFieldProps> = props => {
const classes = useStyles();
const { InputProps, ...otherTextFieldProps } = props;
return (
<TextField
className={classes.textField}
InputProps={{ disableUnderline: true, ...InputProps }}
{...otherTextFieldProps}
/>
);
}; ...and the TypeScript error:
(pretty, huh?) I reasoned that TypeScript might be having trouble combining the explicit assignments and the spread props, so I tried this, and it eliminated the TypeScript error: const MyTextFieldWrapperWithoutTheTypeScriptError: React.FC<TextFieldProps> = props => {
const classes = useStyles();
const { InputProps, ...otherTextFieldProps } = props;
const wrapperProps = {
className: classes.textField,
InputProps: { disableUnderline: true, ...InputProps },
...otherTextFieldProps,
};
return <TextField {...wrapperProps} />;
}; I'm using |
I've had to overcome another, similar issue. This time my component composes several Material-UI components and has a prop named interface MyTextFieldProps {
TextFieldProps?: Partial<TextFieldProps>;
}
// Typescript error on `TextField`
const MyTextField: React.FC<MyTextFieldProps> = (props) => {
const classes = useStyles();
const { InputProps, ...otherTextFieldProps } = props.TextFieldProps ?? {};
return (
<TextField
className={classes.textField}
InputProps={{ disableUnderline: true, ...InputProps }}
{...otherTextFieldProps}
/>
);
}; ...and the TypeScript error:
The error only occurs when I include I have a reproduction codesandbox for those interested (maybe @eps1lon?) To defeat this, I decided to assign export const MyTextField: React.FC<MyTextFieldProps> = props => {
const classes = useStyles();
const { InputProps, variant = 'standard', ...otherTextFieldProps } = props.TextFieldProps ?? {};
const TextFieldInputProps =
variant === 'outlined' ? InputProps : { disableUnderline: true, ...InputProps };
return (
<TextField
className={classes.textField}
variant={variant}
InputProps={TextFieldInputProps}
{...otherTextFieldProps}
/>
);
}; Fun times. Again, hope this helps. |
I am making a component that forwards prop types to material UI components. When doing this with TextField I get complaints about "variant" type being incompatable.
Current Behavior 😯
Error is given in typescript
Expected Behavior 🤔
Would expect no error and it to function in the same way as with other components such as Typography
Steps to Reproduce 🕹
https://codesandbox.io/s/material-ui-typescript-wierdness-yk119
Context 🔦
I have made custom formik inputs. In these instances props such as error, helperText etc. are set by formik hence I want to omit them to prevent intelisense suggesting them as valid props on my fields.
Your Environment 🌎
See codesandbox for more details
The text was updated successfully, but these errors were encountered: