Skip to content
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

[TypographyProps] add "component" property to typescript types #19512

Closed
2 tasks done
srghma opened this issue Feb 1, 2020 · 19 comments
Closed
2 tasks done

[TypographyProps] add "component" property to typescript types #19512

srghma opened this issue Feb 1, 2020 · 19 comments

Comments

@srghma
Copy link
Contributor

srghma commented Feb 1, 2020

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

TypographyProps doesnt have component prop

Expected Behavior 🤔

TypographyProps should have component prop

Steps to Reproduce 🕹

Steps:

import { TypographyProps } from '@material-ui/core/Typography';

let props : TypographyProps = { classes: {} ,  children: [], component: "a" };

does not compile

$ tsc ./test.ts
test.ts:3:62 - error TS2322: Type '{ classes: {}; children: undefined[]; component: string; }' is not assignable to type 'OverrideProps<TypographyTypeMap<{}, "span">, "span">'.
  Object literal may only specify known properties, and 'component' does not exist in type 'OverrideProps<TypographyTypeMap<{}, "span">, "span">'.

3 let props : TypographyProps = { classes: {} ,  children: [], component: "a" };
                                                               ~~~~~~~~~~~~~~


Found 1 error.
  1. second example

this compiles

import React from 'react';
import Typography from '@material-ui/core/Typography';

export default function Types() {
  return (
    <div>
      <Typography variant="h1" component="h2" gutterBottom>
        h1. Heading
      </Typography>
    </div>
  );
}

this compiles too

import React from 'react';
import Typography from '@material-ui/core/Typography';
import { TypographyProps } from '@material-ui/core/Typography';

const attrs: TypographyProps = {
  variant: "h1" ,
  gutterBottom: true,
}

export default function Types() {
  return (
    <div>
      <Typography {...attrs}>
        h1. Heading
      </Typography>
    </div>
  );
}

but this does not

import React from 'react';
import Typography from '@material-ui/core/Typography';
import { TypographyProps } from '@material-ui/core/Typography';

const attrs: TypographyProps = {
  variant: "h1" ,
  component: "h2",
  gutterBottom: true,
}

export default function Types() {
  return (
    <div>
      <Typography {...attrs}>
        h1. Heading
      </Typography>
    </div>
  );
}

error

Type '{ variant: "h1"; component: string; gutterBottom: true; }' is not assignable to type '{ align?: Alignment; color?: "inherit" | "initial" | "primary" | "secondary" | "textPrimary" | "textSecondary" | "error"; display?: "inline" | "initial" | "block"; gutterBottom?: boolean; noWrap?: boolean; paragraph?: boolean; variant?: "button" | ... 13 more ... | "srOnly"; variantMapping?: Partial<...>; } & Common...'.
  Object literal may only specify known properties, and 'component' does not exist in type '{ align?: Alignment; color?: "inherit" | "initial" | "primary" | "secondary" | "textPrimary" | "textSecondary" | "error"; display?: "inline" | "initial" | "block"; gutterBottom?: boolean; noWrap?: boolean; paragraph?: boolean; variant?: "button" | ... 13 more ... | "srOnly"; variantMapping?: Partial<...>; } & Common...'.ts(2322)

2020-02-02-13:26:04-screenshot

but if I do

import React from 'react';
import Typography from '@material-ui/core/Typography';
import { TypographyProps } from '@material-ui/core/Typography';

const attrs: TypographyProps & { component: React.ElementType } = {
  variant: "h1" ,
  component: "h2",
  gutterBottom: true,
}

export default function Types() {
  return (
    <div>
      <Typography {...attrs}>
        h1. Heading
      </Typography>
    </div>
  );
}

it compiles again

Context 🔦

https://github.com/dwhitney/purescript-react-basic-mui/issues/28#issuecomment-581070959

Your Environment 🌎

Tech Version
Material-UI master
React
Browser
TypeScript
@srghma
Copy link
Contributor Author

srghma commented Feb 1, 2020

#19510

@oliviertassinari oliviertassinari added the support: Stack Overflow Please ask the community on Stack Overflow label Feb 1, 2020
@support
Copy link

support bot commented Feb 1, 2020

👋 Thanks for using Material-UI!

We use GitHub issues exclusively as a bug and feature requests tracker, however,
this issue appears to be a support request.

For support, please check out https://material-ui.com/getting-started/support/. Thanks!

If you have a question on StackOverflow, you are welcome to link to it here, it might help others.
If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

@eps1lon
Copy link
Member

eps1lon commented Feb 1, 2020

Please fill out the template.

@support support bot closed this as completed Feb 1, 2020
@srghma
Copy link
Contributor Author

srghma commented Feb 1, 2020

I found, there is no issue on Material-UI side

@srghma srghma changed the title [Typography] add "component" property to typescript types [TypographyProps] add "component" property to typescript types Feb 2, 2020
@srghma
Copy link
Contributor Author

srghma commented Feb 2, 2020

@eps1lon how can I reopen this issue, I have updated the description, it's material-ui issue

@srghma
Copy link
Contributor Author

srghma commented Feb 2, 2020

so, component: React.ElementType should be added either:

  • to TypographyProps
  • or to OverrideProps (because maybe not only TypographyProps have this bug, AND because OverridableComponent does that, it merges { component: C } with other props
export interface OverridableComponent<M extends OverridableTypeMap> {
  <C extends React.ElementType>(props: { component: C } & OverrideProps<M, C>): JSX.Element;
  (props: DefaultComponentProps<M>): JSX.Element;
}

)

what is better?

@srghma
Copy link
Contributor Author

srghma commented Mar 20, 2020

Yes, other components are affected too

e.g. this is not possible

import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import { AvatarProps } from '@material-ui/core/Avatar';

const attrs: AvatarProps = {
  alt: "My Alt" ,
  component: "span", // Type '{ alt: string; component: string; }' is not assignable to type '{ alt?: string; imgProps?: ImgHTMLAttributes<HTMLImageElement>; sizes?: string; src?: string; srcSet?: string; variant?: "circle" | "rounded" | "square"; } & CommonProps<AvatarTypeMap<{}, "div">> & Pick<...>'.
// Object literal may only specify known properties, and 'component' does not exist in type '{ alt?: string; imgProps?: ImgHTMLAttributes<HTMLImageElement>; sizes?: string; src?: string; srcSet?: string; variant?: "circle" | "rounded" | "square"; } & CommonProps<AvatarTypeMap<{}, "div">> & Pick<...>'
}

export default function Types() {
  return (
    <div>
      <Avatar {...attrs}>
        H
      </Avatar>
    </div>
  );
}

but this is

import React from 'react';
import Avatar from '@material-ui/core/Avatar';

export default function Types() {
  return (
    <div>
      <Avatar alt="My Alt" component="span">
        H
      </Avatar>
    </div>
  );
}

thus it makes sense to fix OverrideProps

srghma added a commit to srghma/material-ui that referenced this issue Mar 20, 2020
srghma added a commit to srghma/material-ui that referenced this issue Mar 20, 2020
@support support bot removed the support: Stack Overflow Please ask the community on Stack Overflow label Mar 20, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 20, 2020

When diving in #20191, I was wondering if developers building on top of the xxxProps wouldn't face this problem, it seems that they do. Something is off with the TypographyProps type, it doesn't account for the default component (DefaultComponentProps) nor the component prop support of the Typography component. Basically, it seems that we can no longer use these types for wrapping. For instance:

import React from 'react';
import Typography, { TypographyProps }  from '@material-ui/core/Typography';

export default function Types(props: TypographyProps) {
  return (
      <Typography {...props}>
        h1. Heading
      </Typography>
  );
}

@StevenVerbiest
Copy link

StevenVerbiest commented May 6, 2020

I'm having the same issue when using the Typography component as a styled component:

// Property 'component' does not exist on type [...]
<StyledTypography variant="h3" component="h2">
    Organization
</StyledTypography>
export const StyledTypography = styled(Typography)``

Edit
Not sure if my issue was relevant, but I managed to find a fix for the styled-components issue: #13921 (comment)

@nstolmaker
Copy link

I made a hacky solution for Typography, check out my comment on this other issue

@oliviertassinari
Copy link
Member

I'm closing for now. Let's wait for a new report on v5.

@zeestack
Copy link

zeestack commented Mar 2, 2022

This issue still exist in v5. What is the best possible way to reopen it?

@jesus-escalona
Copy link

jesus-escalona commented Apr 21, 2022

This issue still exist in v5. What is the best possible way to reopen it?

This helped me.
#13921 (comment)

@ManuC84
Copy link

ManuC84 commented Jun 28, 2022

This still happens. Is there a fix if one does not want to use styled ?

@srghma 's last example actually worked for me as a fix (thanks a lot)

@cyrilchapon
Copy link

I'm getting another issue, but I suspect it being intimately related to the same core issue.

const ModuleTitle: FunctionComponent<TypographyProps> = (props) => (
  <Typography variant='h5' {...props} />
)

const Index = (props) => (
  <Container>
    <ModuleTitle>Hey</ModuleTitle>
  </Container>
)

Gives me

const ModuleTitle: FunctionComponent<Partial<Record<"typography" | "fontFamily" | "fontSize" | "fontStyle" | "fontWeight" | "letterSpacing" | "lineHeight" | "textAlign" | "textTransform", any>>>
Type '{ children: string; }' has no common property with type 'IntrinsicAttributes & Partial<Record<"typography" | "fontFamily" | "fontSize" | "fontStyle" | "fontWeight" | "letterSpacing" | "lineHeight" | "textAlign" | "textTransform", any>>'.

(trying not to reopen, just tell me if unrelated, I'll delete that comment)

@Alesich
Copy link

Alesich commented Sep 6, 2022

I also have the same issue with:
"@mui/material": "^5.9.3",
"@mui/styles": "^5.10.2",

TS2322: Type '{ children: string; variant: "subtitle2"; component: string; }' is not assignable to type 'IntrinsicAttributes & TypographyProps'.   Property 'component' does not exist on type 'IntrinsicAttributes & TypographyProps'.

@spmsupun
Copy link

spmsupun commented Jun 5, 2023

@oliviertassinari any news on this?

@justasdev
Copy link

justasdev commented Jun 6, 2023

This definitely should not be closed. We are still experiencing this issue with TypographyProps as well as other component's props with component prop.

@francescovenica
Copy link

any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests