Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我应该为react组件指定什么类型?_Javascript_Reactjs_Typescript_Types_Typeof - Fatal编程技术网

Javascript 我应该为react组件指定什么类型?

Javascript 我应该为react组件指定什么类型?,javascript,reactjs,typescript,types,typeof,Javascript,Reactjs,Typescript,Types,Typeof,这个问题很直截了当。 我来自Qt/C++背景,因此我认为显式是一种很好的实践,因此 protectedsanitycheck():void{…}//智能且漂亮 受保护的sanityCheck(){…}//愚蠢且总是错误的 那么,我该如何明确地说: protectedtofoobar():????? { 返回(foobar); } 当我查看文档时,它什么也没说。当我检查typeof时,它只是说object 这就是答案,还是我可以更具体一点 谢谢。React确实为React中的功能组件提供了类型

这个问题很直截了当。 我来自Qt/C++背景,因此我认为显式是一种很好的实践,因此

protectedsanitycheck():void{…}//智能且漂亮
受保护的sanityCheck(){…}//愚蠢且总是错误的
那么,我该如何明确地说:

protectedtofoobar():?????
{
返回(foobar);
}
当我查看文档时,它什么也没说。当我检查typeof时,它只是说object

这就是答案,还是我可以更具体一点


谢谢。

React确实为React中的功能组件提供了类型 你可以这样做

const toFoobar: React.FunctionComponent<IFoobar> = ({propsType}) => ();
const-toFoobar:React.FunctionComponent=({propsType})=>();
这在React.SFC之前的React中是新的


希望它有帮助

正确的类型应该是
JSX.Element
。这就是JSX语法的计算结果。如果使用React,
JSX.Element
只是对
React.Element

的类型引用,IFoobar应该是什么?它的接口IFoobar{name:string//type of props}不起作用<代码>类型“Element[]”未提供与签名匹配的内容“(props:PropsWithChildren,context?:any):ReactElement ReactElement Component)>| null)|(新的(props:any)=>Component)>|null”。TS2322可能是因为我的道具界面是通用的。那么我的代码行应该是什么呢?唯一有效的方法是
protectedtofoobar():JSX.Element[]
,这似乎很奇怪。@akiva否,
JSX.Element
Type'Element[]缺少类型'Element'中的以下属性:类型、道具、按键TS2739
否我意识到你是对的;数组工作的原因是我的return语句是一个映射表达式。