简体   繁体   English

如何阅读 typescript 错误以了解应使用哪种类型?

[英]How to read typescript error in order to understand what type should be used?

I don't understand how to handle typescript error like this.我不明白如何处理这样的 typescript 错误。
I am using react-navigation and tried to make custom header.我正在使用 react-navigation 并尝试自定义 header。
From documentation there are list of parameters that this component get but I don't know how to type check it.从文档中有该组件获取的参数列表,但我不知道如何键入检查它。

interface CustomHeaderProps {
  navigation: any;
  route: any;
  options: any;
  back: any;
}

const CustomHeader: FC<CustomHeaderProps> = ({
  navigation,
  route,
  options,
  back,
}) => {...

I set that header in navigator:我在导航器中设置了 header:

<HomeStack.Navigator
      screenOptions={{
        header: props => <CustomHeader {...props} />,
      }}>

And here I get error:在这里我得到错误:

(alias) const CustomHeader: React.FC import CustomHeader Type '{ back?: { title: string; (别名)const CustomHeader: React.FC import CustomHeader Type '{ back?: { title: string; } | } | undefined;不明确的; options: NativeStackNavigationOptions;选项:NativeStackNavigationOptions; route: Route<string, object |路线:路线<字符串,object | undefined>;未定义>; navigation: NativeStackNavigationProp<ParamListBase, string, undefined>;导航:NativeStackNavigationProp<ParamListBase, string, undefined>; }' is not assignable to type 'CustomHeaderProps'. }' 不可分配给类型 'CustomHeaderProps'。 Property 'back' is optional in type '{ back?: { title: string;属性 'back' 在类型 '{ back?: { title: string; 中是可选的} | } | undefined;不明确的; options: NativeStackNavigationOptions;选项:NativeStackNavigationOptions; route: Route<string, object |路线:路线<字符串,object | undefined>;未定义>; navigation: NativeStackNavigationProp<ParamListBase, string, undefined>;导航:NativeStackNavigationProp<ParamListBase, string, undefined>; }' but required in type 'CustomHeaderProps'. }' 但在类型 'CustomHeaderProps' 中是必需的。

Can somebody help me how to understand this kind off errors and how to set type.有人可以帮助我如何理解这种关闭错误以及如何设置类型。

The error should be given already formatted like this:错误应该已经格式化如下:

(alias) const CustomHeader: React.FC
import CustomHeader
Type '{ back?: { title: string; } | undefined; options: NativeStackNavigationOptions; route: Route<string, object | undefined>; navigation: NativeStackNavigationProp<ParamListBase, string, undefined>; }' is not assignable to type 'CustomHeaderProps'.
    Property 'back' is optional in type '{ back?: { title: string; } | undefined; options: NativeStackNavigationOptions; route: Route<string, object | undefined>; navigation: NativeStackNavigationProp<ParamListBase, string, undefined>; }' but required in type 'CustomHeaderProps'.

Usually they're always like this:通常他们总是这样:

Offending code
Type '...' is not assignable to type '...'
    Reason why it isn't
        Reason why the reason is why
            Reason why the reason why the reason is why
                ...

Here the reason is just simply Property 'back' is not optional in type '...' but required in type '...' .原因很简单Property 'back' is not optional in type '...' but required in type '...' Most of the times the true reason is last and it helps the most, but in more verbose errors, you should go up the "reason stack" to find out the root cause.大多数时候,真正的原因排在最后并且帮助最大,但在更详细的错误中,您应该 go 向上“原因堆栈”找出根本原因。

So for the actual error, it's complaining that the property is optional in the type of props you gave but it isn't in the definition.因此,对于实际错误,它抱怨该属性在您提供的props类型中是可选的,但它不在定义中。

(Not all TypeScript errors are in this format. Only these kinds of type mismatch errors are like this.) (不是所有的TypeScript错误都是这种格式,只有这几种类型不匹配错误是这样的。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM