Skip to content

Commit 57c238b

Browse files
committed
[Typography] Fix typecheck for Typography with custom component (#18473)
1 parent 3bbc68d commit 57c238b

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

packages/material-ui/src/Typography/Typography.d.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
import * as React from 'react';
22
import { StandardProps, PropTypes } from '..';
3+
import { OverrideProps, OverridableTypeMap, OverridableComponent } from '../OverridableComponent';
34
import { ThemeStyle } from '../styles/createTypography';
45

56
type Style = ThemeStyle | 'srOnly';
67

7-
export interface TypographyProps
8-
extends StandardProps<React.HTMLAttributes<HTMLElement>, TypographyClassKey> {
9-
align?: PropTypes.Alignment;
10-
color?:
11-
| 'initial'
12-
| 'inherit'
13-
| 'primary'
14-
| 'secondary'
15-
| 'textPrimary'
16-
| 'textSecondary'
17-
| 'error';
18-
component?: React.ElementType<React.HTMLAttributes<HTMLElement>>;
19-
display?: 'initial' | 'block' | 'inline';
20-
gutterBottom?: boolean;
21-
noWrap?: boolean;
22-
paragraph?: boolean;
23-
variant?: Style | 'inherit';
24-
variantMapping?: Partial<Record<Style, string>>;
8+
export interface TypographyTypeMap<P = {}, D extends React.ElementType = 'span'> {
9+
props: P & {
10+
align?: PropTypes.Alignment;
11+
color?:
12+
| 'initial'
13+
| 'inherit'
14+
| 'primary'
15+
| 'secondary'
16+
| 'textPrimary'
17+
| 'textSecondary'
18+
| 'error';
19+
display?: 'initial' | 'block' | 'inline';
20+
gutterBottom?: boolean;
21+
noWrap?: boolean;
22+
paragraph?: boolean;
23+
variant?: Style | 'inherit';
24+
variantMapping?: Partial<Record<Style, string>>;
25+
};
26+
defaultComponent: D;
27+
classKey: TypographyClassKey;
2528
}
2629

30+
declare const Typography: OverridableComponent<TypographyTypeMap>;
31+
32+
export type TypographyProps<
33+
D extends React.ElementType = TypographyTypeMap['defaultComponent'],
34+
P = {}
35+
> = OverrideProps<TypographyTypeMap<P, D>, D>;
36+
2737
export type TypographyClassKey =
2838
| 'root'
2939
| 'h1'
@@ -54,6 +64,4 @@ export type TypographyClassKey =
5464
| 'displayInline'
5565
| 'displayBlock';
5666

57-
declare const Typography: React.ComponentType<TypographyProps>;
58-
5967
export default Typography;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { FC } from 'react';
2+
import { Typography } from '@material-ui/core';
3+
4+
const TypographyTest = () => {
5+
const CustomComponent: React.FC<{ prop1: string; prop2: number }> = () => <div />;
6+
return (
7+
<div>
8+
<Typography />
9+
<Typography align="inherit" color="inherit" display="block" />
10+
<Typography align="left" color="initial" display="inline" />
11+
<Typography align="right" color="primary" display="initial" />
12+
<Typography align="justify" color="secondary" display="initial" />
13+
<Typography align="inherit" color="textPrimary" />
14+
<Typography align="inherit" color="textSecondary" />
15+
<Typography align="inherit" color="error" />
16+
// $ExpectError
17+
<Typography display="incorrectValue" />
18+
<Typography component="a" href="url" display="block" />
19+
<Typography component="label" htmlFor="html" display="block" />
20+
// $ExpectError
21+
<Typography component="a" href="url" display="incorrectValue" />
22+
// $ExpectError
23+
<Typography component="a" incorrectAttribute="url" />
24+
// $ExpectError
25+
<Typography component="incorrectComponent" href="url" />
26+
// $ExpectError
27+
<Typography component="div" href="url" />
28+
// $ExpectError
29+
<Typography href="url" />
30+
<Typography component={CustomComponent} prop1="1" prop2={12} />
31+
// $ExpectError
32+
<Typography component={CustomComponent} prop1="1" prop2={12} id="1" />
33+
// $ExpectError
34+
<Typography component={CustomComponent} prop1="1" />
35+
// $ExpectError
36+
<Typography component={CustomComponent} prop1="1" prop2="12" />
37+
</div>
38+
);
39+
};

0 commit comments

Comments
 (0)