geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
https://geoman.io
MIT License
2.21k stars 433 forks source link

Typescript error when removing custom button from toolbar. #1079

Closed EliezerB123 closed 2 years ago

EliezerB123 commented 2 years ago

Hi!

Bug:

See: Related bug: https://github.com/geoman-io/leaflet-geoman/issues/771 JSFiddle: https://codesandbox.io/s/typescript-errors-with-custom-buttons-7o06f?file=/src/index.ts

When removing a custom button using map.pm.addControls({'myButton':false}), typescript gives a compilation error, because arbitrary strings are not allowed as parameters. The only strings typescript allows are the items in ToolbarOptions, none of which are arbitrary strings:

  position?: L.ControlPosition;
  positions?: BlockPositions;
  drawMarker?: boolean;
  drawCircleMarker?: boolean;
  drawPolyline?: boolean;
  drawRectangle?: boolean;
  drawPolygon?: boolean;
  drawCircle?: boolean;
  editMode?: boolean;
  dragMode?: boolean;
  cutPolygon?: boolean;
  removalMode?: boolean;
  rotateMode?: boolean;
  oneBlock?: boolean;
  drawControls?: boolean;
  editControls?: boolean;
  customControls?: boolean;
  optionsControls?: boolean;
  pinningOption?: boolean;
  snappingOption?: boolean;

Expected result:

map.pm.addControls(
      {'myButton':false}
);

should not cause an Typescript compilation error.


(Unrelated side note: This is extremely helpful and the documentation could definitely use a clarification, to say that addControls() can REMOVE buttons as well. It's extremely unclear from the documentation, and I probably would never have known if I hadn't run into the issue https://github.com/geoman-io/leaflet-geoman/issues/771#issuecomment-764809723 .)

Edit: Added better demo. Old one didn't properly error.

Falke-Design commented 2 years ago

Thanks for reporting, will be fixed!

EliezerB123 commented 2 years ago

I added a better demo: https://codesandbox.io/s/typescript-errors-with-custom-buttons-7o06f?file=/src/index.ts