简体   繁体   English

打字稿中可选的道具类型检查错误

[英]Error in optional prop-types check in typescript

I'm writing react typescript and have problem with optional prop-types check.我正在编写 react typescript 并且在可选的prop-types 检查方面有问题。 Below is my code:下面是我的代码:

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)]),
};

Somehow my typescript throw error in className when declare like this.不知何故,当这样声明时,我的打字稿在 className 中抛出错误。 Here is the error:这是错误:

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'.

This looks weird because as I read in the documents, the prop-types should be optional and accept undefined .这看起来很奇怪,因为当我在文档中阅读时, prop-types应该是可选的并接受undefined Anyone know how to fix this, thanks.哪位大神知道怎么解决,谢谢。

Update : I think the problem come from PropTypes.arrayOf(PropTypes.string) .更新:我认为问题来自PropTypes.arrayOf(PropTypes.string) The arrayOf cause the error. arrayOf导致错误。

My tsconfig.json我的tsconfig.json

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "module": "commonjs",
        "target": "es5",
        "lib": ["es2015", "dom"],
        "types": ["node", "jest"],
        "jsx": "react",
        "moduleResolution": "node",
        "sourceMap": true,
        "allowJs": false,
        "strict": true,
        "declaration": true,
        "esModuleInterop": true
    },
    "exclude": ["src/**/__tests__/**"]
}

Typescript version : "typescript": "^3.4.2"打字稿版本: "typescript": "^3.4.2"

You should set exactly the types of your optional values when they might be undefined or null in typescript.当您的可选值在打字稿中可能undefined或为null时,您应该准确设置它们的类型。 If you are in strict mode you have two options for this situation:如果您处于strict模式下,您有两种选择:

1. Set the type for undefined s and null s 1. 为undefined s 和null s 设置类型

interface InputNumberProps {
  className?: undefined | string | (string | undefined | null)[];
}  

2. Change your tsconfig to skip checking of null and undefined : 2. 更改您的tsconfig以跳过检查nullundefined

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "module": "commonjs",
        "target": "es5",
        "lib": ["es2015", "dom"],
        "types": ["node", "jest"],
        "jsx": "react",
        "moduleResolution": "node",
        "sourceMap": true,
        "allowJs": false,
        "strict": true,
        "strictNullChecks": false, // this will skip that checking 
        "declaration": true,
        "esModuleInterop": true
    },
    "exclude": ["src/**/__tests__/**"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM