簡體   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