sweetalert: Typings error TS2403: Subsequent variable declarations must have the same type.

Upgraded to Angular 7 w/ TypeScript 3.2.4 and am now getting the following whenever I compile:

import swal from 'sweetalert';

swal({ ... });

ERROR in node_modules/sweetalert/typings/sweetalert.d.ts(4,9): error TS2403: Subsequent variable declarations must have the same type. Variable ‘swal’ must be of type ‘typeof import(“C:/Projects/me/browser/node_modules/sweetalert/typings/sweetalert”)’, but here has type ‘SweetAlert’.

If I edit node_modules/sweetalert/typings/sweetalert.d.ts from…

import swal, { SweetAlert } from "./core";

declare global {
  const swal: SweetAlert;
  const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;

… to …

import swal, { SweetAlert } from "./core";

export default swal;
export as namespace swal;

… it starts working.

Any ideas?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 26
  • Comments: 25

Commits related to this issue

Most upvoted comments

Hi all,

Save yourself the effort and use sweetalert 2 https://sweetalert2.github.io/

@bogdan-calapod Here’s what I have been doing as a workaround, so that our CI build doesn’t fail:

  1. Download Sweetalert minified script file here (right-click and Save link as sweetalert.min.js).
  2. Place sweetalert.min.js in src/assets/scripts in Angular application.
  3. Add src/assets/scripts/sweetalert.min.js to scripts entry in angular.json
  4. In any components/services you want to use Sweetalert, add declare var swal: any; above the class declaration.
  5. You can use Sweetalert anywhere in the file using global variable swal, i.e. return swal({...})
  6. You can now remove Sweetalert NPM package since you are using local script file: npm uninstall sweetalert --save

I fixed this by overriding the sweetalert types. I added a file in the src dir named src/node_modules/sweetalert/index.d.ts

//this file is needed because the sweetalert typings need to be overwritten because they are broken
//the real fix here is to stop using sweetalert

declare global {
  const _swal: any;
  const sweetAlert: any;
}

export default _swal;
export as namespace swal;

Any idea on a workaround apart from editing the definition file manually? It’s becoming a bit of a hassle remembering to do that after every clone / npm install.

Same issue, same situation.

However, I noticed that just commenting out the line export as namespace swal; also lets it compile. I’ve been looking at the TypeScript changelogs for any clues…

Solved it by changing this line:

import swal from 'sweetalert';

to:

const swal = require('sweetalert');

or (with the interface):

import { SweetAlert } from 'sweetalert/typings/core';
const swal: SweetAlert = require('sweetalert');