我使用的是@types/react-simple-maps,在他们的一个类型定义中:
interface GeographiesChildrenArgument {
geographies: object[];
path: GeoPath;
projection: GeoProjection;
}他们使用object[]
现在,当我尝试使用这些定义时,我必须这样做:
{({ geographies }): React.ReactNodeArray => geographies.map(
(geo: object) => (
...有没有办法让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[]
发布于 2020-02-08 01:00:46
这是一个愚蠢的答案,但最终的解决方案是,我刚刚促成了@types/react-simple-maps将object改为any,因为在他们的指南中建议使用any而不是object,因为object太严格了。
发布于 2020-01-11 09:06:15
我会尝试扩展接口。您不能更改现有属性的类型。类似于下面的内容。如果它起作用了,请告诉我。还可以看看这篇文章,它是关于修改现有类型的Overriding interface property type defined in Typescript d.ts file
{({ geographies }: { geographies: object[] extends GeographyType[] }): React.ReactNodeArray =>
geographies.map(
(geo: GeographyType) => (
...https://stackoverflow.com/questions/59689891
复制相似问题