繁体   English   中英

类型“未定义”不可分配给类型“架构”<unknown> '。 是的</unknown>

[英]Type 'undefined' is not assignable to type 'Schema<unknown>'. Yup

我不确定我是否收到此错误,有人知道如何解决这个问题吗? 下面是我的代码,错误发生在extra: Yup.array().of(Yup.lazy((value)....

const claimFormValidationSchema = Yup.object().shape<ClaimApplicationForm>({
  id: Yup.number().notRequired(),
  claimType: Yup.mixed().required(),
  claimAmount: Yup.number().required('Claim Amount is Required'),
  currencyCode: Yup.string().required(),
  rate: Yup.number().required('Rate is Required'),
  convertedAmount: Yup.number().required('Converted Amount is Required'),
  extra: Yup.array().of(
    Yup.lazy((value) => {
      if ('required' in value) {
        if (value.required === true) {
          return Yup.object().shape({
            value: Yup.string().required(),
          });
        } else {
          return Yup.object().shape({
            value: Yup.string().notRequired(),
          });
        }
      }
    }),
  ),
});

错误:

'(值:未知)类型的参数 => ObjectSchema> | undefined' 不能分配给类型为 '(value: unknown) => Schema' 的参数。 键入“对象架构> | undefined' 不可分配给类型 'Schema'。 类型“未定义”不可分配给类型“架构”。

你的问题出在extra财产上

  extra: Yup.array().of(
    Yup.lazy((value) => {
      // outter if
      if ('required' in value) {
        if (value.required === true) {
          return Yup.object().shape({
            value: Yup.string().required(),
          });
        } else {
          return Yup.object().shape({
            value: Yup.string().notRequired(),
          });
        }
      }          
      // end outter if

      // if reaches here, nothing is returned
    }),
  ),

你需要在Yup.lazy中返回一些东西,但是如果这个条件不是 true 'required' in value ,你将不会返回任何东西,给你undefined和那个错误。

我不确定你需要返回什么,但它一定是一些东西。

一段时间后,我得到了我期望做的事情。

extra: Yup.array().of(
  Yup.lazy((data: any) => {
    if ('required' in data) {
      if (data.required === true) {
        return Yup.object().shape({
          value: Yup.string().required(),
        });
      } else {
        return Yup.object().shape({
          value: Yup.string().notRequired(),
        });
      }
    }
  }),
),

我通过像这样为“额外”设置架构来解决这个问题。

输入'元素| 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.

相关问题 类型 &#39;unknown&#39; 不能分配给类型 &#39;(prevState: number | undefined) =&gt; number | 不明确的&#39; 输入'ListRenderItem<iphotos> ' 不可分配给类型 'ListRenderItem<unknown></unknown></iphotos> 我试图在本机反应上做 bottomSheet 但我收到错误。 错误是:类型“string”不可分配给类型“Ref”<unknown> | 不明确的'</unknown> 输入&#39;元素| undefined&#39; 不可分配给类型 &#39;FC<any> | 不明确的&#39; “typeof QuestionInfo”类型的参数不可分配给“SetStateAction”类型的参数<QuestionInfo | undefined> &#39; 输入'元素| 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,> “声音”类型的参数不能分配给“SetStateAction”类型的参数<undefined> &#39; 'Promise 类型的参数<item | undefined> []' 不可分配给“SetStateAction”类型的参数<item[]> '</item[]></item> 输入'{ rippleColor: any; }' 不可分配给类型 'ColorValue | null | 不明确的' 类型“字符串”不可分配给类型“ImageSourcePropType”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM