Skip to content

Commit e5df40a

Browse files
committed
slightly better types on the specs
1 parent 4adcadb commit e5df40a

File tree

7 files changed

+28
-20
lines changed

7 files changed

+28
-20
lines changed

packages/angular-skyhook/src/connection-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export interface ConnectionBase<TMonitor> extends SubscriptionLike {
9191
* To create one, refer to {@link SkyhookDndService#dropTarget}.
9292
*/
9393
export interface DropTarget<
94-
Item extends {} = {},
95-
DropResult extends {} = {}
94+
Item = {},
95+
DropResult = {}
9696
> extends ConnectionBase<DropTargetMonitor<Item, DropResult>> {
9797

9898
/** Use this method to have a dynamically typed target. If no type has
@@ -125,7 +125,7 @@ To create one, refer to {@link SkyhookDndService#dragSource}.
125125
*/
126126
export interface DragSource<
127127
Item,
128-
DropResult extends {} = {}
128+
DropResult = {}
129129
> extends ConnectionBase<DragSourceMonitor<Item, DropResult>> {
130130

131131
/** Use this method to have a dynamically typed source. If no type has

packages/angular-skyhook/src/connector.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export class SkyhookDndService {
131131
* {@link DropTarget#setTypes} in a lifecycle hook.
132132
*/
133133
public dropTarget<
134-
Item extends {} = {},
135-
DropResult extends {} = {}
134+
Item = {},
135+
DropResult = {}
136136
>(
137137
types: TypeOrTypeArray | null,
138138
spec: DropTargetSpec<Item, DropResult>,
@@ -185,7 +185,7 @@ export class SkyhookDndService {
185185

186186
public dragSource<
187187
Item,
188-
DropResult extends {} = {}
188+
DropResult = {}
189189
>(
190190
type: string | symbol | null,
191191
spec: DragSourceSpec<Item, DropResult>,

packages/angular-skyhook/src/drag-source-specification.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
import { DragSourceMonitor } from './source-monitor';
77

8+
/*
9+
Note that you can't infer Item if you supply both beginDrag and endDrag,
10+
since endDrag needs to find some type to put in the 'monitor' argument.
11+
But people can always nail it down with `dnd.dragSource<MyItemType>('BOX', { ...spec })`.
12+
Related: https://github.com/Microsoft/TypeScript/issues/19345
13+
*/
14+
815
export interface DragSourceSpec<
916
Item,
10-
DropResult extends {} = {}
17+
DropResult = {}
1118
> {
1219

1320
/**
@@ -21,13 +28,14 @@ export interface DragSourceSpec<
2128
* this.id }` from this method.
2229
*
2330
*/
24-
beginDrag(monitor: DragSourceMonitor<Item, DropResult>): Item;
31+
// erase Item here because inference doesn't know what it is yet
32+
beginDrag: (monitor: DragSourceMonitor<void, void>) => Item;
2533

2634
/**
2735
* Optional. Queries your component to determine whether this source can be
2836
* dragged. Default returns true; this is often sufficient.
2937
*/
30-
canDrag?(monitor: DragSourceMonitor<Item, DropResult>): boolean;
38+
canDrag?: (monitor: DragSourceMonitor<void, void>) => boolean;
3139

3240
/** By default, only the drag source that initiated the drag operation is
3341
* considered to be dragging. You might override this by matching on the
@@ -45,7 +53,7 @@ export interface DragSourceSpec<
4553
* immediately, and if you use `NgZone.run()` then you may experience
4654
* performance degradation..
4755
*/
48-
isDragging?(monitor: DragSourceMonitor<Item, DropResult>): boolean;
56+
isDragging?: (monitor: DragSourceMonitor<Item, void>) => boolean;
4957

5058
/**
5159
* Optional. Notifies your component when dragging ends.
@@ -54,5 +62,5 @@ export interface DragSourceSpec<
5462
* want to check {@link DragSourceMonitor#didDrop} and {@link DragSourceMonitor#getDropResult} for more
5563
* details.
5664
*/
57-
endDrag?(monitor: DragSourceMonitor<Item, DropResult>): void;
65+
endDrag?: (monitor: DragSourceMonitor<Item, DropResult>) => void;
5866
}

packages/angular-skyhook/src/drop-target-specification.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export interface DropTargetSpec<
1919
*
2020
* Default, when not specified, is `true`.
2121
**/
22-
canDrop?(monitor: DropTargetMonitor<Item, DropResult>): boolean;
22+
canDrop?: (monitor: DropTargetMonitor<Item, DropResult>) => boolean;
2323

2424
/** Called frequently while the mouse hovers over the owner drop target while
2525
* dragging a relevant item.
2626
*
2727
* */
28-
hover?(monitor: DropTargetMonitor<Item, DropResult>): void;
28+
hover?: (monitor: DropTargetMonitor<Item, DropResult>) => void;
2929

3030
/** Called when a compatible item is dropped on the target. You may either
3131
* return nothing, or a plain object.
@@ -44,6 +44,6 @@ export interface DropTargetSpec<
4444
* This method will not be called if `canDrop()` is defined and returns `false`.
4545
*/
4646

47-
drop?(monitor: DropTargetMonitor<Item, DropResult>): DropResult | void;
47+
drop?: (monitor: DropTargetMonitor<Item, DropResult>) => DropResult | void;
4848

4949
}

packages/angular-skyhook/src/monitor-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { Offset } from './type-ish';
99
import { Observable } from 'rxjs';
1010

11-
export interface MonitorBase<Item extends {} = {}> {
11+
export interface MonitorBase<Item> {
1212

1313
/** The type of the item in transit. Returns `null` if no item is being dragged. */
1414
getItemType(): string | symbol | null;

packages/angular-skyhook/src/source-monitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import { MonitorBase } from './monitor-base';
77

88
export interface DragSourceMonitor<
9-
Item extends {} = {},
10-
DropResult extends {} = {}
9+
Item = {},
10+
DropResult = {}
1111
> extends MonitorBase<Item> {
1212

1313
/**
@@ -47,7 +47,7 @@ Instead, keep your `canDrag` logic simple, and replicate it in your template.
4747
* `drop()` overrides the child drop result previously set by the child.
4848
* Returns `null` if called outside `endDrag()`.
4949
*/
50-
getDropResult(): Object & any;
50+
getDropResult(): DropResult;
5151

5252
/**
5353
* Returns `true` if some drop target handled the `drop` event; `false`

packages/angular-skyhook/src/target-monitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import { MonitorBase } from './monitor-base';
77

88
export interface DropTargetMonitor<
9-
Item extends {} = {},
10-
DropResult extends {} = {}
9+
Item = {},
10+
DropResult = {}
1111
> extends MonitorBase<Item> {
1212
/**
1313
* Returns `true` if there is a drag operation in progress, and the owner's

0 commit comments

Comments
 (0)