Тип «число» нельзя назначить типу «строка» - PullRequest
0 голосов
/ 03 октября 2018

У меня есть такая ошибка:

Типу 'число' нельзя присвоить типу 'строка'.

Ошибка в этом месте:

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

Как это исправить?

Полный код:

    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')
      }
    })

1 Ответ

0 голосов
/ 03 октября 2018

Node.textContent является строкой, а swal.getTimerLeft() не возвращает строку.

Чтобы исправить это, используйте Number.prototype.toString():

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