【发布时间】:2021-01-31 17:39:37
【问题描述】:
我正在开始一个新的 Typescript 和 React 项目,我在这两种语言方面都有经验,但事实证明将它们结合起来相当困难。
我现在面临的问题是,当我想接收一个 id 用作道具并使用 PropTypes 定义时,当我使用 InferProps 获取组件的类型时,它会将 id 类型显示为“string | null | undefined" 并且 div 组件只接受 "string | null"。
import PropTypes, { InferProps } from 'prop-types';
export function TestComponent({ id }: InferProps<typeof TestComponent.propTypes>) {
return <div id={id}></div>;
}
TestComponent.propTypes = {
id: PropTypes.string
};
我想过直接在 PropTypes.string 中更改类型,但一定有更好的解决方案。
感谢您的帮助。
【问题讨论】:
标签: reactjs typescript react-proptypes