[英]Typescript: Using boolean type is not accepted by TS compiler
Right now, I put isLoading
to any
, but if I put it to boolean
(like I want it to be), it throws the following error when hovering over the "Loading" const.现在,我把
isLoading
放到any
,但是如果我把它放到boolean
(就像我想要的那样),当悬停在“Loading”常量上时,它会抛出以下错误。 As far as I understand, this should be working.据我了解,这应该有效。 Very new to Typescript, so please be gentle.
对 Typescript 非常陌生,所以请温柔。 Any help would be appreciated.
任何帮助,将不胜感激。
package.json package.json
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"typescript": "^4.0.3"
Error message错误信息
Type '{ ({ isLoading, size, color, }: LoadingProps): false | JSX.Element; defaultProps: Partial<LoadingProps> | undefined; }' is not assignable to type 'FC<LoadingProps>'.
Type 'false | Element' is not assignable to type 'ReactElement<any, any> | null'.
Type 'false' is not assignable to type 'ReactElement<any, any> | null'.ts(2322)
index.tsx索引.tsx
import React from 'react';
import ClipLoader from 'react-spinners/ClipLoader';
interface LoadingProps {
isLoading: any;
size?: number;
color?: string;
}
/**
* Loading icon to let user know a promise is going on
*
* @param isLoading Passing true to show spinner, and pass false to remove it
* @param size react-spinner parameter for size of icon
* @param color Color of Loading icon
*/
const Loading: React.FC<LoadingProps> = ({
isLoading,
size = 35,
color = '#F6F7FB',
}: LoadingProps) => {
return isLoading && <ClipLoader size={size} color={color} />;
};
Loading.defaultProps = {
size: 35,
color: '#F6F7FB',
};
export default Loading;
When isLoading
is false
your return statement returns false
, which is not a ReactElement
.当
isLoading
为false
时,您的 return 语句返回false
,这不是ReactElement
。
I'd suggest always using a ternary for conditional rendering instead.我建议始终使用三元进行条件渲染。 Like:
喜欢:
return isLoading ? <ClipLoader size={size} color={color} /> : null
See Conditional Rendering in docs for more details.有关更多详细信息,请参阅文档中的条件渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.