有没有比创建自己的验证器更简单的方法来定义多个条件 Prop 类型?

Is there an easier way to define multiple condition Prop Types than by making own validator?

如果我希望 prop 是数字类型并且同时需要怎么办?据我所知,我必须为此制作自己的验证器。有没有更简单优雅的方法?

function ShopxItem(props) {
    return (
        <>
            {props.name}
            {props.price}
        </>
    )
}

function PropTypesExample(){
    return (
        <>
            <ShopxItem name="1" price="xdd"></ShopxItem>
        </>
    )
}


ShopxItem.propTypes = {
//i would want to do it in that way but this overwrite "price" and returning an error "Duplicate Key xxx"
    price: PropTypes.number,
    price: PropTypes.isRequired,

// but as far as i know i have to do this like that
    price: (props, propName, componentName) => {
        let error;
        const prop = props[propName];
        if(prop === undefined || isNaN(prop)){error = new Error(`${componentName} price is required and have to be a number`);return error;}
        return null;
    },
}

像这样的东西应该可以工作:

price: PropTypes.oneOfType([PropTypes.number, PropTypes.func]).isRequired

为此,我们改用 air-bnb's or,而不是 oneOfType,但我记不清具体原因了。无论如何,您也可能对该软件包感兴趣。