Search code examples
angularsweetalert2

Type 'number' is not assignable to type 'string'


I have such a mistake:

Type 'number' is not assignable to type 'string'.

Error in this place:

swal.getContent().querySelector('strong').textContent = swal.getTimerLeft()

How can I fix it?

Full code:

    let timerInterval
    swal({
      title: 'Auto close alert!',
      html: 'I will close in <strong></strong> seconds.',
      timer: 2000,
      onOpen: () => {
        swal.showLoading()
        timerInterval = setInterval(() => {
          swal.getContent().querySelector('strong')
            .textContent = swal.getTimerLeft()
        }, 100)
      },
      onClose: () => {
        clearInterval(timerInterval)
      }
    }).then((result) => {
      if (
        // Read more about handling dismissals
        result.dismiss === swal.DismissReason.timer
      ) {
        console.log('I was closed by the timer')
      }
    })

Solution

  • Node.textContent is a string and swal.getTimerLeft() does not return a string.

    To fix this, use Number.prototype.toString():

    Swal.getHtmlContainer().querySelector('strong').textContent = swal.getTimerLeft().toString();