【发布时间】:2019-12-02 02:16:57
【问题描述】:
当可选参数没有默认值时,typescript / typescript-eslint 有什么方法可以呈现错误?我正在尝试将我的 React 代码库从 JSX 转换为 TSX,并且不再有关于未定义 defaultProps 的警告令人担忧。谢谢。
错误:标题没有默认的道具值
import * as React from 'react';
interface Props {
title?: string;
}
const SampleComponent: React.FC<Props> = ({ title }) => (
<h1>
{title && <p>{title}</p>}
</h1>
);
export default SampleComponent;
good:title 有默认的 prop 值
import * as React from 'react';
interface Props {
title?: string;
}
const SampleComponent: React.FC<Props> = ({ title = 'foo' }) => (
<h1>
{title && <p>{title}</p>}
</h1>
);
export default SampleComponent;
【问题讨论】:
标签: javascript reactjs typescript eslint typescript-eslint