D
D
Dmitry Baskakov2020-01-28 06:21:07
typescript
Dmitry Baskakov, 2020-01-28 06:21:07

How to correctly assign the type of variables (events and html element)?

There is a code that works, but the IDE swears:

  • currentElement can be null

export function verifyRequired (event: { target: HTMLInputElement }, ignoreError: boolean = false) {
  updateStepButton(ignoreError)

  const currentElement: HTMLInputElement = event.target
  const parentElement = currentElement.parentNode.querySelector('span')

  if (currentElement.getAttribute('required') && (!currentElement.value || currentElement.value.length < 2)) {
    parentElement.classList.add('show-tooltip')
  } else {
    parentElement.classList.remove('show-tooltip')
  }
}


How to fix this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sun_Day, 2020-01-28
@Sun_Day

Write it like this:

const parentElement = currentElement.parentNode!.querySelector('span')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question