我正在编写反应打字稿,并且对可选的道具类型检查有问题。下面是我的代码:
import PropTypes from 'prop-types';
interface InputNumberProps {
className?: string | string[];
}
export const InputNumberAutosize = (props: InputNumberProps, ref: refType) => {
...
}
const InputNumberAutoSizeComponent = React.forwardRef(InputNumberAutosize);
InputNumberAutoSizeComponent.propTypes = {
className: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
};
不知何故,当我这样声明时,我的打字稿会在 className 中抛出错误。这是错误:
TS2322: Type 'Requireable<string | (string | null | undefined)[]>' is not assignable to type 'Validator<string | string[] | null | undefined>'.
Types of property '[nominalTypeHack]' are incompatible.
Type '{ type: string | (string | null | undefined)[] | null | undefined; } | undefined' is not assignable to type '{ type: string | string[] | null | undefined; } | undefined'.
Type '{ type: string | (string | null | undefined)[] | null | undefined; }' is not assignable to type '{ type: string | string[] | null | undefined; }'.
Types of property 'type' are incompatible.
Type 'string | (string | null | undefined)[] | null | undefined' is not assignable to type 'string | string[] | null | undefined'.
Type '(string | null | undefined)[]' is not assignable to type 'string | string[] | null | undefined'.
Type '(string | null | undefined)[]' is not assignable to type 'string[]'.
Type 'string | null | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
这看起来很奇怪,因为当我在文档中阅读时,prop-types应该是可选的并接受undefined。有谁知道如何解决这个问题,谢谢。
呼如林
相关分类