clauderic / clauderic/dnd-kit

isSortable type guards use "any" in return type

Open Beginner friendly
#2,132 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
17.6k
Forks
924
Avg merge
2d 10h
Merged PRs (30d)
2

Description

I'm working in a build environment that's fairly strict about our use of `any`.

I've found that the `isSortable()` type guard isn't very helpful in that environment because all it guarantees is `SortableDraggable` | `SortableDroppable`. We need something that can return `SortableDraggable | SortableDroppable` where `T` is a known subtype of `Data`.

For now, I've created alternate versions of `isSortable()` and `isSortableOperation()` that do this. I'm wondering if you'd consider integrating our changes into dnd-kit?

```ts
import { Data, Draggable, DragOperation, Droppable } from "@dnd-kit/abstract";
import { SortableDraggable, SortableDroppable } from "@dnd-kit/dom/sortable";

export function isSortable(
element: Draggable | null,
): element is SortableDraggable;
export function isSortable(
element: Droppable | null,
): element is SortableDroppable;
export function isSortable(
element: Draggable | Droppable | null,
): element is SortableDroppable | SortableDraggable {
return element instanceof SortableDroppable || element instanceof SortableDraggable;
}

export function isSortableOperation(
operation: DragOperation, Droppable>,
): operation is DragOperation, SortableDroppable> {
return isSortable(operation.source) && isSortable(operation.target);
}
```

Contributor guide

Open the contributing guide

Research direction

Start at the TypeScript definitions of isSortable() and isSortableOperation(), using the shown Data, Draggable, Droppable, and DragOperation types to trace their current guards. Verify that a known T is preserved in both return types and that the package’s existing type checks pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.