【发布时间】:2019-10-16 13:24:23
【问题描述】:
我正在尝试将 Material-ui 中的 Typography 组件与 TypeScript 一起使用,但我收到了这个奇怪的错误
TypeScript error: Type 'string' is not assignable to type 'ComponentClass<HTMLAttributes<HTMLElement>, any> | FunctionComponent<HTMLAttributes<HTMLElement>> | undefined'. TS2322
8 | }
9 | export default ({ text, date }: Props) => (
> 10 | <Typography component="p" gutterBottom>
| ^
11 | {text && <span>{text}: </span>}
12 | <FormattedDate value={date} />
13 |
这是我的组件的样子
import React from 'react';
import { FormattedDate, FormattedTime } from 'react-intl';
import Typography from '@material-ui/core/Typography';
interface Props {
date: Date;
text?: string;
}
export default ({ text, date }: Props) => (
<Typography component="p" gutterBottom>
{text && <span>{text}: </span>}
<FormattedDate value={date} />
<FormattedTime value={date} />
</Typography>
);
我无法理解为什么 "p" 不是 component 属性的可接受值。我用 "h1" 和 "h2" 尝试了它以同样的方式失败,显然,official demo 也使用了字符串。
我有什么遗漏吗?我不想用// @ts-ignore 忽略这个,但想解决这个问题。
【问题讨论】:
标签: javascript reactjs typescript material-ui