Skip to content

v1.0.5: type annotations on connections

Compare
Choose a tag to compare
@cormacrelf cormacrelf released this 19 Jun 05:06
· 770 commits to master since this release

You can now do this:

interface InTransit {
  id: number;
}
// ...

// note the <InTransit>
source = this.dnd.dragSource<InTransit>('ITEM', {
  beginDrag: () => ({ id: this.id }),
  isDragging: monitor => {
    // getItem returns InTransit
    return monitor.getItem().id === this.id;
  }
});
// item$ is an Observable<InTransit>
item$ = this.source.listen(m => m.getItem());

target = this.dnd.dropTarget<InTransit>('ITEM', {
  drop: monitor => {
    // getItem returns InTransit
    const { id } = monitor.getItem();
  }
});

By default, the connection creators will try to infer your type from the beginDrag return type.

However, limitations on TypeScript prevent inferring where there is an endDrag method as well. See the comment on DragSourceSpec for why.

It won't cause an error, however; it will just infer that Item is {}.

Lastly, you can even specify the getDroppedItem type, with the second type parameter.