如何解决我的TypeScript错误:类型'void'上不存在属性'then'

时间:2017-11-15 07:02:52

标签: typescript

我正在使用Sweet Alert库:

https://limonte.github.io/sweetalert2/

我试图使用网站上的以下代码:

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false
}).then(function () {
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  )
}, function (dismiss) {
  // dismiss can be 'cancel', 'overlay',
  // 'close', and 'timer'
  if (dismiss === 'cancel') {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )
  }
})

我已经通过运行安装了类型:

npm install --save @types/sweetalert

但是我仍然收到与使用'then'有关的错误消息:

error TS2339: Property 'then' does not exist on type 'void'.

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我认为你的类型定义文件和你的库没有加起来。您已提取@types/sweetalert,即https://sweetalert.js.org/的.d.ts文件,而不是https://limonte.github.io/sweetalert2/

查看SweetAlert2的存储库,它在与库相同的npm包中包含type definitions