简体   繁体   English

Typescript:TS 编译器不接受使用 boolean 类型

[英]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 .isLoadingfalse时,您的 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.

相关问题 使用TypeScript 2.0-&gt; TS2345在React中处理多个输入:类型&#39;{[x:string]:string | 布尔; }” - Handing multiple inputs in React with TypeScript 2.0 -> TS2345: Argument of type '{ [x: string]: string | boolean; }' Typescript 编译器给出“&#39;type aliases&#39; 只能在 .ts 文件中使用。” 在一个js文件中 - Typescript compiler giving “ 'type aliases' can only be used in a .ts file.” in a js file 为什么Typescript编译器不会将.ts更改为.js? - Why does Typescript compiler not change .ts to .js? 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 Typescript 类型 boolean 不可分配给 void - Typescript type boolean is not assignable to void 打字稿编译器错误 TS2307:找不到模块“jquery” - Typescript Compiler error TS2307: Cannot find module 'jquery' 如何使用 TypeScript 编译器消除错误 TS18003 - How to get rid of error TS18003 with TypeScript compiler 有没有办法放松 TypeScript 编译器警告 TS2339? - Is there a way to relax TypeScript compiler warning TS2339? Typescript:类型“{}”.ts 上不存在属性“set” - Typescript: Property 'set' does not exist on type '{}'.ts JavaScript / TypeScript 上是否有“节点”类型? ts(2345) - Is there a type 'Node' on JavaScript / TypeScript? ts(2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM