【问题标题】:Typescript and react-google-maps type errorsTypescript 和 react-google-maps 类型错误
【发布时间】:2018-12-27 19:33:06
【问题描述】:

我是 Typescript 的初学者,很困惑。我有一个使用 react-google-maps 的组件,该组件是可重用的。

Map.tsx

import * as React from 'react';
import { compose, withStateHandlers } from 'recompose';
import { GoogleMap, withGoogleMap } from 'react-google-maps';

const ContactMap: React.ComponentClass<{}> = compose(
  withStateHandlers(() => ({
    isOpen: false,
    // tslint:disable-next-line:align
  }), {
      onToggleOpen: ({ isOpen }) => () => ({
        isOpen: !isOpen,
      }),
    }),
  withGoogleMap,
)(props =>
  <div>
    <GoogleMap
      defaultZoom={props.zoom}
      defaultCenter={props.center}
      defaultOptions={{
        streetViewControl: false,
        scaleControl: false,
        mapTypeControl: false,
        panControl: false,
        zoomControl: false,
        rotateControl: false,
        fullscreenControl: false,
        disableDefaultUI: true,
        scrollwheel: false,
      }}
    >
      {props.children}
    </GoogleMap>,
  </div>,
);

export default ContactMap;

错误是:Property 'zoom' does not exist on type '{ children?: ReactNode; }'.,我认为它会为 center 做同样的事情。

我尝试过类似的东西

interface Props {
  containerElement: any;
  mapElement: any;
  zoom: number;
  center: any;
}

然后将其传入,但这并不能解决问题。它返回

Type 'ComponentClass<{}>' is not assignable to type 'ComponentClass<Props>'.
  Type '{}' is not assignable to type 'Props'.
    Property 'containerElement' is missing in type '{}'.

我使用地图的组件:

import * as React from 'react';
import { Map } from '@ecomm/map';
import { InfoWindow, Marker } from 'react-google-maps';

const ContactMap: React.SFC = () => {
  return (
    <Map
      containerElement={<div style={{ height: `400px` }} />}
      mapElement={<div style={{ height: `100%` }} />}
      center={{
        lat: 40.745093,
        lng: -73.993048,
      }}
      zoom={16}
    >
      <Marker
        position={{
          lat: 40.745093,
          lng: -73.993048,
        }}
      >
        <InfoWindow>
          <TextHeader>CHELSEA STUDIO</TextHeader>
        </InfoWindow>
      </Marker>
    </Map>
  );
};

export default ContactMap;

我不确定要离开这里,这很令人沮丧。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: reactjs typescript react-google-maps


    【解决方案1】:

    要解决像您这样的问题,最好检查您使用的函数的定义。 从您的 sn-p 中,很容易猜到问题出在 compose 的使用上,因为它是一个接受一个组件并返回另一个组件的函数。 有了这些知识,我们可以去https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/recompose/index.d.ts 或检查您的 node_modules 类型。这里有

    export function compose<TInner, TOutter>(
         ...functions: Function[]
    ): ComponentEnhancer<TInner, TOutter>;
    
    interface ComponentEnhancer<TInner, TOutter> {
         (component: Component<TInner>): ComponentClass<TOutter>;
    }
    

    所以基本上你可以传递两种类型来告诉你的 innerProps 和 outerProps 是什么

    interface InnerProps {
      zoom: number;
      center: number[]; // it will not be available in ContactMap as prop
    }
    interface OuterProps {
      zoom: number;
    }
    const ContactMap = compose<InnerProps , OuterProps>(
      withStateHandlers(() => ({
        isOpen: false,
        // tslint:disable-next-line:align
      }), {
          onToggleOpen: ({ isOpen }) => () => ({
            isOpen: !isOpen,
          }),
        }),
      withGoogleMap,
    )(props =>
      <div>
        <GoogleMap
          defaultZoom={props.zoom}
          defaultCenter={props.center}
          defaultOptions={{
            streetViewControl: false,
            scaleControl: false,
            mapTypeControl: false,
            panControl: false,
            zoomControl: false,
            rotateControl: false,
            fullscreenControl: false,
            disableDefaultUI: true,
            scrollwheel: false,
          }}
        >
          {props.children}
        </GoogleMap>,
      </div>,
    );
    ...
    <ContactMap  
       zoom={5}
       center={[1,3]} // error because it doesnt exist in OuterProps
    />
    

    当然,如果它们是相同的compose&lt;Props, Props&gt;,你可以传递相同的道具两次

    你不需要像这样声明变量const ContactMap: React.ComponentClass&lt;{}&gt;的类型,因为TS会自动从函数的结果中提取它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2022-06-21
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多