ThibaultJanBeyer / DragSelect

An easy JavaScript library for selecting and moving elements. With no dependencies. Drag-Select & Drag-And-Drop. – Examples:
https://dragselect.com/
Other
715 stars 81 forks source link

Area ts type #184

Closed eddie-englund closed 1 year ago

eddie-englund commented 1 year ago

Describe the bug The type of "area" is:

type area = Document | HTMLElement | SVGElement | undefined;

It should also have null in the union type because document.getElementById's type is:

type elById = HTMLElement | null;

To Reproduce Install the package and add an area like in the docs: https://dragselect.com/docs/guided-examples/Area Expected behavior It should not throw a type error.

Screenshots image

ThibaultJanBeyer commented 1 year ago

Thank you,

will add it to the typescript rewrite https://github.com/ThibaultJanBeyer/DragSelect/pull/174.

Actually it should never be null. null is a special value to force-unset the param. So setting null as area will break DragSelect. So these types are correct to warn you that the element has to be available.

In your example typescript warns you that the element might be null, if that is the case, you should not pass it as area. But if you know that the element you are selecting is always available, you should use type cast it to the correct type. i.e. as HTMLElement if it is a HTMLElement.

Interestingly enough in my quick test document.getElementById('something') is actually always HTMLElement 🤔

image