当可选参数没有默认值时,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;
好:标题有默认的道具值
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;