简体   繁体   English

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

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

This code:这段代码:

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

...produces this compilation error when compiling with TypeScript: ...在使用 TypeScript 编译时产生此编译错误:

error TS2345: Argument of type 'Element' is not assignable to parameter of type 'ReactElement'.错误 TS2345:“Element”类型的参数不可分配给“ReactElement”类型的参数。 Type 'null' is not assignable to type 'ReactElement类型“null”不可分配给类型“ReactElement”

This only happens when the strictNullChecks TypeScript compilation flag is set to true .只有当strictNullChecks TypeScript 编译标志设置为true时才会发生这种情况。

Yes I could disable that flag, but I want it on for added compile-time checking/safety.是的,我可以禁用该标志,但我希望启用它以增加编译时检查/安全性。

If I change the last line to this:如果我将最后一行更改为:

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

...it works with the flag set to true . ...它适用于设置为true的标志。

I've recently tried migrating from "plain" JavaScript to TypeScript in a React project and this is tripping up all my tests, so adding all the TypeScript type assertions to all these occurrences in the test code will be a pain.我最近尝试在 React 项目中从“纯”JavaScript 迁移到 TypeScript,这会阻碍我的所有测试,因此将所有 TypeScript 类型断言添加到测试代码中的所有这些事件将是一件痛苦的事情。

Is this a bug, or do I have no other choice?这是一个错误,还是我别无选择?

If you have JSX inside your component or component test file must have .tsx as your file extension.如果您的组件或组件测试文件中有 JSX,则必须将.tsx作为文件扩展名。 My code was not compiling as I had the .ts file extension.我的代码没有编译,因为我有 .ts 文件扩展名。 The moment I renamed it to .tsx / jsx it started working nicely!我将它重命名为.tsx / jsxjsx它开始工作得很好!

Please also note that I was using Typescript, so that's why I renamed it to .tsx .另请注意,我使用的是 Typescript,这就是我将其重命名为.tsx的原因。 In case you are using ES6 rename it to .jsx .如果您使用 ES6, .jsx其重命名为.jsx

This is apparently an issue which was introduced in the 15.0.5 React type definitions.这显然是 15.0.5 React 类型定义中引入的一个问题。 If you change to 15.0.4, everything should be fine.如果您更改为 15.0.4,一切都应该没问题。

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

This happens when I don't return JSX inside of React Fragment or empty tags.当我不在 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 <></>;
};

When I added empty tags everything worked as expected.当我添加空标签时,一切都按预期工作。

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

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

    return <></>;
};

Also, if we remove the return <></> statement error will be shown.此外,如果我们删除return <></>语句,则会显示错误。 Component, as I wrote, always need to return correct JSX component.正如我所写,组件总是需要返回正确的 JSX 组件。

Versions版本

  • React: 17.0.2反应:17.0.2
  • @types/react: 17.0.34 @类型/反应:17.0.34
  • TypeScript: 4.6.4打字稿: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,> - Typescript: Type 'Element[]' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<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,> - Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; - JSX element type 'ReactElement<any> is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'Element | null' &#39;jquery&#39;类型的参数不能分配给&#39;element&#39;类型的参数 - argument of type 'jquery' is not assignable to parameter of type 'element' 类型“ any []”的参数不能分配给类型“ A”的参数。 类型“ any []”中缺少属性“ a” - Argument of type 'any[]' is not assignable to parameter of type 'A'. Property 'a' is missing in type 'any[]' &#39;{ period: any; 类型的参数价格:任意; }&#39; 不可分配给类型为 &#39;MAInput&#39; 的参数 - Argument of type '{ period: any; prices: any; }' is not assignable to parameter of type 'MAInput' &#39;{ [key: string]: any; 类型的参数 }&#39; 不可分配给“any[]”类型的参数 - Argument of type '{ [key: string]: any; }' is not assignable to parameter of type 'any[]' 参数类型不能分配给类型的参数 - Argument type is not assignable to parameter of type &#39;ReadableStream 类型的参数<any> &#39; 不可分配给类型为 &#39;ReadableStream&#39; 的参数 - Argument of type 'ReadableStream<any>' is not assignable to parameter of type 'ReadableStream' x 类型的参数不能分配给 '{ x: any; 类型的参数。 }' - Argument of type x is not assignable to parameter of type '{ x: any; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM