繁体   English   中英

“Element”类型的参数不可分配给“ReactElement”类型的参数<any></any>

[英]Argument of type 'Element' is not assignable to parameter of type 'ReactElement<any>

这段代码:

import * as React from 'react';                                                                                              
                                                                                 
const Component = () => <div/>;                                                  
                
function culprit<P>(node: React.ReactElement<P>) {
  console.log(node);
}
                                                             
culprit(<Component/>);

...在使用 TypeScript 编译时产生此编译错误:

错误 TS2345:“Element”类型的参数不可分配给“ReactElement”类型的参数。 类型“null”不可分配给类型“ReactElement”

只有当strictNullChecks TypeScript 编译标志设置为true时才会发生这种情况。

是的,我可以禁用该标志,但我希望启用它以增加编译时检查/安全性。

如果我将最后一行更改为:

culprit( (<Component/> as React.ReactElement<any>) );

...它适用于设置为true的标志。

我最近尝试在 React 项目中从“纯”JavaScript 迁移到 TypeScript,这会阻碍我的所有测试,因此将所有 TypeScript 类型断言添加到测试代码中的所有这些事件将是一件痛苦的事情。

这是一个错误,还是我别无选择?

如果您的组件或组件测试文件中有 JSX,则必须将.tsx作为文件扩展名。 我的代码没有编译,因为我有 .ts 文件扩展名。 我将它重命名为.tsx / jsxjsx它开始工作得很好!

另请注意,我使用的是 Typescript,这就是我将其重命名为.tsx的原因。 如果您使用 ES6, .jsx其重命名为.jsx

这显然是 15.0.5 React 类型定义中引入的一个问题。 如果您更改为 15.0.4,一切都应该没问题。

看到这个问题: https : //github.com/DefinitelyTyped/DefinitelyTyped/pull/14284

当我不在 React Fragment 或空标签中返回 JSX 时,就会发生这种情况。

const ErrorMessage: FC<ErrorProps> = (props) => {
    if (!isEmpty(props.errors)) {

        return Object.entries(props.errors).map((item) => (
            <Typography component="span">{item[0]}</Typography>
        ))
    };

    return <></>;
};

当我添加空标签时,一切都按预期工作。

const ErrorMessage: FC<ErrorProps> = (props) => {
    if (!isEmpty(props.errors)) {

        return (
            <>
                {Object.entries(props.errors).map((item) => (
                    <Typography component="span">{item[0]}</Typography>
                ))}
            </>
        )
    };

    return <></>;
};

此外,如果我们删除return <></>语句,则会显示错误。 正如我所写,组件总是需要返回正确的 JSX 组件。

版本

  • 反应:17.0.2
  • @类型/反应:17.0.34
  • 打字稿:4.6.4

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,>

[英]Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

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

相关问题 Typescript:类型“Element[]”不可分配给类型“ReactElement” <any, string | jsxelementconstructor<any> >'</any,> 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; &#39;jquery&#39;类型的参数不能分配给&#39;element&#39;类型的参数 类型“ any []”的参数不能分配给类型“ A”的参数。 类型“ any []”中缺少属性“ a” &#39;{ period: any; 类型的参数价格:任意; }&#39; 不可分配给类型为 &#39;MAInput&#39; 的参数 &#39;{ [key: string]: any; 类型的参数 }&#39; 不可分配给“any[]”类型的参数 参数类型不能分配给类型的参数 &#39;ReadableStream 类型的参数<any> &#39; 不可分配给类型为 &#39;ReadableStream&#39; 的参数 x 类型的参数不能分配给 '{ x: any; 类型的参数。 }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM