【问题标题】:TypeScript Parameter Defintion OverridesTypeScript 参数定义覆盖
【发布时间】:2020-04-28 14:07:32
【问题描述】:

我正在使用@types/react-simple-maps,并在他们的一个类型定义中:

interface GeographiesChildrenArgument {
    geographies: object[];
    path: GeoPath;
    projection: GeoProjection;
}

他们使用object[]

现在,当我尝试使用这些定义时,我必须这样做:

{({ geographies }): React.ReactNodeArray => geographies.map(
  (geo: object) => (
    ...

有没有办法让对象更具体?如果我尝试:

{({ geographies }: { geographies: GeographyType[] }): React.ReactNodeArray => geographies.map(
  (geo: GeographyType) => (
    ...

然后我收到以下错误:

Type '({ geographies }: { geographies: GeographyType[]; }) => React.ReactNodeArray' is not assignable to type '((data: GeographiesChildrenArgument) => void) | (((data: GeographiesChildrenArgument) => void) & string) | (((data: GeographiesChildrenArgument) => void) & number) | ... 5 more ... | undefined'.
Type '({ geographies }: { geographies: GeographyType[]; }) => React.ReactNodeArray' is not assignable to type '(data: GeographiesChildrenArgument) => void'.
Types of parameters '__0' and 'data' are incompatible.
Type 'GeographiesChildrenArgument' is not assignable to type '{ geographies: GeographyType[]; }'.
Types of property 'geographies' are incompatible.
Type 'object[]' is not assignable to type 'GeographyType[]'.
Type '{}' is missing the following properties from type 'GeographyType': type, geometry, propertiests (2322)
index.d.ts(95, 5): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & GeographiesProps & { children?: ReactNode; }'

那么,有没有办法覆盖object[] 成为我的对象定义:GeographyType[]

【问题讨论】:

标签: javascript reactjs typescript


【解决方案1】:

我会尝试扩展接口。您不能更改现有属性的类型。 下面是这样的。让我知道它是否有效。 还要检查一下它是关于修改现有类型Overriding interface property type defined in Typescript d.ts file

 {({ geographies }: { geographies: object[] extends GeographyType[] }): React.ReactNodeArray => 
  geographies.map(
   (geo: GeographyType) => (
  ...

【讨论】:

  • 我试过那个,如果你尝试这个,你会得到这个错误:Type '({ geographies }: { geographies: any; }) => React.ReactNodeArray' is not assignable to type '(geographies: object[], projection: GeoProjection) => void'.
【解决方案2】:

这是一个愚蠢的答案,但最终的解决方案是我只是为@types/react-simple-maps 做出了贡献,将object 更改为any,因为在他们的指南中建议使用any 而不是object 作为object太严格了。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2018-05-18
    • 1970-01-01
    • 2021-01-03
    • 2018-07-24
    • 2019-09-29
    • 1970-01-01
    • 2018-05-08
    • 2022-11-05
    相关资源
    最近更新 更多