Skip to content

Instantly share code, notes, and snippets.

@danvk
Created September 25, 2019 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvk/673a427ba5155ad60d46cd3e6317a64b to your computer and use it in GitHub Desktop.
Save danvk/673a427ba5155ad60d46cd3e6317a64b to your computer and use it in GitHub Desktop.
Errors surfaced in DOM code (for Effective TypeScript)
function handleDrag(eDown: Event) {
const targetEl = eDown.currentTarget;
targetEl.classList.add('dragging');
// ~~~~~~~ Object is possibly 'null'.
// ~~~~~~~~~ Property 'classList' does not exist on type 'EventTarget'.
const dragStart = [
eDown.clientX, eDown.clientY
// ~~~~~~~ 'clientX' does not exist on 'Event'.
// ~~~~~~~ 'clientY' does not exist on 'Event'.
];
const handleUp = (eUp: Event) => {
targetEl.classList.remove('dragging');
// ~~~~~~~~ Object is possibly 'null'
// ~~~~~~~~~ Property 'classList' does not exist on type 'EventTarget'
targetEl.removeEventListener('mouseup', handleUp);
// ~~~~~~~~ Object is possibly 'null'.
const dragEnd = [
eUp.clientX, eUp.clientY
// ~~~~~~~ Property 'clientX' does not exist on 'Event'.
// ~~~~~~~ Property 'clientY' does not exist on 'Event'.
];
console.log('dx, dy = ', [0, 1].map(i => dragEnd[i] - dragStart[i]));
}
targetEl.addEventListener('mouseup', handleUp);
// ~~~~~~~ Object is possibly 'null'.
}
const div = document.getElementById('surface');
div.addEventListener('mousedown', handleDrag);
// ~~~ Object is possibly 'null'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment