-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
The type of styled-component does not match for "strict: true" #13921
Comments
There's some documentation on the TypeScript Guide page related to using type widening with |
Same issue here. In case it's helpful file with issue is:
And tsconfig is:
|
These problems can also be overridden by style unless it is This happens because interface is defined as follows, but you can also see the idea of the author. export interface BaseTextFieldProps
extends StandardProps<FormControlProps, TextFieldClassKey, 'onChange' | 'defaultValue'> {
autoComplete?: string;
autoFocus?: boolean;
children?: React.ReactNode;
defaultValue?: string | number;
disabled?: boolean;
error?: boolean;
FormHelperTextProps?: Partial<FormHelperTextProps>;
fullWidth?: boolean;
helperText?: React.ReactNode;
id?: string;
InputLabelProps?: Partial<InputLabelProps>;
inputRef?: React.Ref<any> | React.RefObject<any>;
label?: React.ReactNode;
margin?: PropTypes.Margin;
multiline?: boolean;
name?: string;
onChange?: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>;
placeholder?: string;
required?: boolean;
rows?: string | number;
rowsMax?: string | number;
select?: boolean;
SelectProps?: Partial<SelectProps>;
type?: string;
value?: Array<string | number | boolean> | string | number | boolean;
}
export interface StandardTextFieldProps extends BaseTextFieldProps {
variant?: 'standard';
InputProps?: Partial<StandardInputProps>;
inputProps?: StandardInputProps['inputProps'];
}
export interface FilledTextFieldProps extends BaseTextFieldProps {
variant: 'filled';
InputProps?: Partial<FilledInputProps>;
inputProps?: FilledInputProps['inputProps'];
}
export interface OutlinedTextFieldProps extends BaseTextFieldProps {
variant: 'outlined';
InputProps?: Partial<OutlinedInputProps>;
inputProps?: OutlinedInputProps['inputProps'];
}
export type TextFieldProps = StandardTextFieldProps | FilledTextFieldProps | OutlinedTextFieldProps; So I solved this problem as follows.
import * as React from "react";
import {
default as MUITextFiled,
StandardTextFieldProps,
FilledTextFieldProps,
OutlinedTextFieldProps
} from "@material-ui/core/TextField";
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export const StandardTextField: React.ComponentType<Omit<StandardTextFieldProps, "variant">> = props => (
<MUITextFiled {...props} variant="standard" />
);
export const FilledTextField: React.ComponentType<Omit<FilledTextFieldProps, "variant">> = props => (
<MUITextFiled {...props} variant="filled" />
);
export const OutlinedTextField: React.ComponentType<Omit<OutlinedTextFieldProps, "variant">> = props => (
<MUITextFiled {...props} variant="outlined" />
); From another program, we do not import import Material - UI 's TextField so we will import from this |
The base problem is with type unions i.e. import TextField, { TextFieldProps } from '@material-ui/core/TextField';
const StyledTextField: React.ComponentType<TextFieldProps> = styled(TextField)({}) as any; Not sure if this defeats any type checking in |
This comment has been minimized.
This comment has been minimized.
I think this should be related to DefinitelyTyped/DefinitelyTyped#29832 and possibly DefinitelyTyped/DefinitelyTyped#30942 I also suggest, that this issue is not only causing problems for Every single component is affected.
Please correct me here if this is a different problem. I will open a new issue then. |
I encourage you to use const StyledButton = styled(Button)`` as typeof Button; This will remove any type information for props added by There are both issues with the styled-component types (union props are lost: changing this caused a 20x perf regression in ts 3.4) and TypeScript design limitations (argument inference only considers one function overload). |
@eps1lon by changing to not losing union props you are meaning switching from using |
@radiosterne Sounded like it. The regression was dicovered in 3.4: microsoft/TypeScript#30663 |
@eps1lon that's great; I take it we are now able to substitute |
@radiosterne Have you done some progress on the topic? |
I'm not an expert here, but if anyone else is using Material-UI with Styled Components, and has tried to style a Typography element and is getting the error described here about type 'component' does not exist on type...' Put this somewhere (I have mine in a utils/types.tsx file, hence the export). interface OverridableComponent<M extends OverridableTypeMap> {
<C extends React.ElementType>(props: { component: C, theme?: Theme } & OverrideProps<M, C>): JSX.Element;
(props: { theme?: Theme } & DefaultComponentProps<M>): JSX.Element;
}
export interface TypographyPropsFix extends OverridableComponent<TypographyTypeMap> { } Then import it into the file where you're styling Typography elements: Then you just have to cast StyledComponents definitions that use Typography to the fixed type, e.g.: const StyledBodyParagraphs2 = styled(Typography)`
margin: 0 0 1rem 0;
text-Align: justify;
${props => props.theme.breakpoints.up("md")} {
margin: 0 0rem 1rem 0;
padding: 5px;
}
` as TypographyPropsFix and Voila! Typescript let's you get on with your life! |
@nstolmaker This seems like a more elaborate way to do #13921 (comment) i.e. |
@eps1lon Good question. The way described works fine for Button components, but the Typography component won't work that way. It gives the same error about Component not being defined. |
@eps1lon - Great solution but doesn't seem to work when extending the props with a few extra properties that are filtered out using @emotion/styled forwarding props in Typescript For example, in the Button component of this library. <ButtonRoot ownerState={state} /> I would normally write something like this: const ButtonRoot = styled(ButtonBase, { shouldForwardProp: prop => prop !== 'ownerState' })<{ownerState: OwnerStateProps>({ownerstate}) => ({
// user ownerState in styles
})
<ButtonRoot ownerState={state} /> If I cast it as a |
@Tiberriver256 has highlighted our exact problem with the previously mentioned solutions of simply using Did you ever figure out a workaround @Tiberriver256 ? |
Just in case it helps anyone else, I've figured this out for our two edge cases where
|
@jdrucey is right, it's still a problem with custom props. To be honest, I end up writing less code and find it easier to maintain if I just do: const Title = styled(Typography)<{
// fix for `component` typing
// see: https://github.com/mui/material-ui/issues/13921
component: React.ElementType
withImage: boolean
}>(({ theme, withImage }) => ({
// etc...
})) MUI uses I feel like this shouldn't be an issue, though. |
When we use styled-component in TypeScript to inherit the Material-UI component, props type does not match.
How do I overwrite a style with styled-component?
There is a sample on how to inherit in the case of styled-component in the following link, but this sample will generate an error if it is set to
strict: true
in tsconfig setting.https://material-ui.com/guides/interoperability/#styled-components
The way I tried is to cast the component with React.SFC and then pass the props type of the material-ui component to that generics.
in this case
The above error will be displayed :(
It was able to solve by the following method.
However, I do not think this is a nice implementation. Required Props
It is because you have to re-implement the interface every time it changes.
@material-ui/core version: 3.6.2
@material-ui/icons version: 3.0.1
styled-components version: 4.1.2
typescript version: 3.2.2
macOS: 10.13.6
The text was updated successfully, but these errors were encountered: