@
@
@Richswitch2019-09-19 08:51:08
typescript
@Richswitch, 2019-09-19 08:51:08

TS2345 how to get around the error?

Hey!
There is this code:

interface PopupInfoObj {
  city?: string,
  image?: string,
  longitude?: number | null,
  latitude?: number | null,
  population?: string,
  state?: string
}
interface State {
  width: number,
  height: number,
  latitude: number,
  longitude: number,
  zoom: number,
  popupInfo?: PopupInfoObj | null | void ;
}

const App: React.FC<Props> = props => {
  const initialState: State = {
    width: 400,
    height: 400,
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8,
    popupInfo: {
      longitude: null,
      latitude: null
    },
  }
  const [viewport, setViewport] = useState(initialState)

  const _renderCityMarker = (city: PopupInfoObj, index: number) => {
    return (
      <Marker
        key={`marker-${index}`}
        longitude={city.longitude}
        latitude={city.latitude}
      >
        <CityPin 
           size={20} 
           onClick={() => setViewport({ popupInfo: city })}     <------- ts(2345) текст ошибки ниже
         />
      </Marker>
    )
  }

return (...)

Thus, when I try to add new city data for the popupInfo TS object , it gives an error
5d83173abab68653188577.png
What should I do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
Unknown, 2019-09-19
_

I managed to curse everyone in the world. But the mistake was the fault of my crooked hands
setViewport({...viewport, popupInfo: cityObj})

L
lnked, 2019-09-19
@lnked

Try

const [viewport, setViewport] = useState<State>(initialState)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question