首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript参数定义覆盖

TypeScript参数定义覆盖
EN

Stack Overflow用户
提问于 2020-01-11 06:40:05
回答 2查看 218关注 0票数 1

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

代码语言:javascript
复制
interface GeographiesChildrenArgument {
    geographies: object[];
    path: GeoPath;
    projection: GeoProjection;
}

他们使用object[]

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

代码语言:javascript
复制
{({ geographies }): React.ReactNodeArray => geographies.map(
  (geo: object) => (
    ...

有没有办法让object更具体化?如果我尝试:

代码语言:javascript
复制
{({ geographies }: { geographies: GeographyType[] }): React.ReactNodeArray => geographies.map(
  (geo: GeographyType) => (
    ...

但随后我得到了以下错误:

代码语言:javascript
复制
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[]

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-08 01:00:46

这是一个愚蠢的答案,但最终的解决方案是,我刚刚促成了@types/react-simple-mapsobject改为any,因为在他们的指南中建议使用any而不是object,因为object太严格了。

票数 0
EN

Stack Overflow用户

发布于 2020-01-11 09:06:15

我会尝试扩展接口。您不能更改现有属性的类型。类似于下面的内容。如果它起作用了,请告诉我。还可以看看这篇文章,它是关于修改现有类型的Overriding interface property type defined in Typescript d.ts file

代码语言:javascript
复制
 {({ geographies }: { geographies: object[] extends GeographyType[] }): React.ReactNodeArray => 
  geographies.map(
   (geo: GeographyType) => (
  ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59689891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档