react-dnd / react-dnd

Drag and Drop for React
http://react-dnd.github.io/react-dnd
MIT License
20.92k stars 1.99k forks source link

TouchBackendOptions should be exported and usable #1473

Open cormacrelf opened 5 years ago

cormacrelf commented 5 years ago

Is your feature request related to a problem? Please describe. The typings for the createTouchBackend function erase the TouchBackendOptions. It is re-typed as a BackendFactory, which means its options? argument is simply any.

https://github.com/react-dnd/react-dnd/blob/15b5c0da0c66db594b90814d007d9a2e6caaea95/packages/core/touch-backend/src/index.ts#L5-L9

That means you don't get type checking when you supply the options. Not even if you create a backend like this:

const MyTouchBackend: BackendFactory = (manager, context) => {
    return TouchBackend(manager, context, {
        enableMouseEvents: false,
        /* ... no type checking here ... */
    });
}

Describe the solution you'd like BackendFactory could take an optional type parameter to describe the type of the options to be passed in. Then you wouldn't have to erase the type:

export type BackendFactory<Options = any> = (..., options?: Options, ...);

const createTouchBackend: BackendFactory<TouchBackendOptions> = ...;
export { TouchBackendOptions, AngleRange } from './interfaces'; // need to export these too
export default createTouchBackend;

This would not be a breaking change, as BackendFactory alone is still valid.

Describe alternatives you've considered A less tidy solution would be to remove the re-typing, because createTouchBackend already conforms to the BackendFactory interface, but this wouldn't be a type-checked guarantee. And see below for more functionality it would miss.

Additional context The old TouchBackend was typed as TouchBackend: (options: TouchBackendOptions) => BackendFactory;, where that argument was basically curried. That enabled typing in the same spot, whereas now, the options type has to be carried around a bit in the type system so it can appear in the right place. For react-dnd, the two places are createDragDropManager and DndProvider's options property. The type could be carried across to the options property, such that it would have to match the backend property. E.g.:

 export type DndProviderProps<BackendContext, BackendOptions> =
    | {
            manager: DragDropManager
      }
    | {
-           backend: BackendFactory
+           backend: BackendFactory<BackendOptions>
            context?: BackendContext
            options?: BackendOptions
            debugMode?: boolean
      }

For @angular-skyhook, that's in the SkyhookDndModule.forRoot function (I haven't updated this yet).

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

cormacrelf commented 5 years ago

stalebot pls

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

cormacrelf commented 4 years ago

Stalebot is the worst for actual issues. I get a reminder nobody has paid any attention to the issue, but I'm not the one that needs reminding.