I'm trying to use stopPropagation method. I use d3.js with d3.d.ts (downloaded via typings
). Is it possible to call stopPropagation
somehow? I used event.stopPropagation()
until I've run my application in firefox. Typings does have d3.event
but it doesn't contain declaration of mentioned method.
Asked
Active
Viewed 1.3k times
6

Julian Rubin
- 1,175
- 1
- 11
- 23
2 Answers
12
It wasn't that hard actually. For stopPropagation
I needed:
(d3.event as Event).stopPropagation();
The typings says that:
event: Event | BaseEvent
So essentialy event can be any of those. I've forgotten how union works in typescript. As the type
property is only one common among Event
and BaseEvent
it was displayed solely. The one case when I needed cast to BaseEvent
(I believe it is only used by d3) was on the dragstart
event.

Julian Rubin
- 1,175
- 1
- 11
- 23
-
Of all the answers.... the easiest works (d3.event as Event).stopPropogation().... good answer – Drenai Jul 17 '16 at 17:44
-
I think the typings might not be working for event anyhow – Drenai Jul 17 '16 at 17:44
2
You can narrow the type by doing a type guard style check:
if (d3.event instanceof Event) {
}
More : https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.html

basarat
- 261,912
- 58
- 460
- 511