5

I am trying to use sweet alert in my angular project.

That's how I use sweet alert:

import swal from 'sweetalert';

swal({
    title: "Problem",
    text: "Try again later!",
    icon: "error"
  })

I get the following error:

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:/Users/user/Desktop/University/Thesis/workspace/web/myProject/project/node_modules/sweetalert/typings/sweetalert")', but here has type 'SweetAlert'.

Anyone can help me with that?

Paraskevas Louka
  • 223
  • 1
  • 3
  • 10

8 Answers8

12

The simple solution to compile the Angular project is Go to your project folder \node_modules\sweetalert\typings\sweetalert.d.ts

In this file simply comment the line // const swal: SweetAlert;

and your final file looks like this:

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

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

export default swal;
export as namespace swal;
  • for me too :D, it was an issue with Vue.js but works :D – Manuel Lazo Jan 19 '21 at 20:08
  • It will not solve the problem in the case you delete the node_modules folder and then ```npm i```. You could make a little script that runs before every build to comment that line out, but it would be great if the developers would do that. – Ambrus Tóth Mar 26 '21 at 18:38
4

I had the same issue, my solution was this.

import * as _swal from 'sweetalert';
import { SweetAlert } from 'sweetalert/typings/core';

const swal: SweetAlert = _swal as any;

for some reason the name "swal" show error, if you change alias by "_swal" it should work

Gerardo Cetzal
  • 170
  • 1
  • 8
  • Where did make this change? – Takatalvi Sep 20 '19 at 16:25
  • 1
    at the top of the file, for example when i need to use the sweetalert on a particular component or service. – Gerardo Cetzal Sep 20 '19 at 20:55
  • 1
    `import * as _swal from 'sweetalert'; import { SweetAlert } from 'sweetalert/typings/core'; ` export class MyComponent { ... `updateHospital(hospital: Hospital) { let url = URL_SERVICE + '/hospital/' + hospital._id; url += `?token=${this._userService.token}`; const swal: SweetAlert = _swal as any; return this.http.put(url, hospital).pipe(map((res:any) => { swal('updated', hospital.name, 'success'); return res.hospital; })); }` – Gerardo Cetzal Sep 20 '19 at 20:56
1

It's solved with me when i remove simple line of code existing in :

node_modules/sweetalert/src/sweetalert.d.ts 

Code will be look like this :

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

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

export default swal;
export as namespace swal;

Just remove this line : const swal: SweetAlert;

And you can check this on gitgub : https://github.com/AlbinoDrought/sweetalert-sans-ts-namespace/commit/699f10b8546a524000dd3e3b41bf7a7e599a2666

Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
0

For SweetAlert2 you just should to use this line to import it:

import * as Swal from 'sweetalert2';

then use it this way:

Swal.fire('Hello world!');
Saeed Falsafin
  • 556
  • 6
  • 15
0

I've got no idea what I just did, but apparently, that's what the compiler wanted me to do cause it worked when I had your same problem:

In the file "/node_modules/sweetalert/typings/sweetalert.d.ts", replace the type of swal in the line
"swal: SweetAlert" with "swal: typeof swal"

So, this (before):

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

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

export default swal;
export as namespace swal;

becomes this (after):

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

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

export default swal;
export as namespace swal;
Sarah
  • 346
  • 1
  • 3
  • 11
0

You can try

import Swal from 'sweetalert2'; 

Swal.fire('Error', 'Please Enter Username', 'error');
0

I had the same problem it was due to the way I imported sweetalert it is necessary to put const swal = require('sweetalert'); INSTEAD OF import swal from 'sweetalert';

-2

BEFORE ALL.. I'm recently update node_modules... and some issues appeared. The last of those, was this one!

I tried with npm install sweetalert2.... but the still giving me problems... so the final solution was.... eliminate that line and WORKS FINE, WITHOUT ERRORS..!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129