3

I'm using @types/swiper in Angular 6 project and I'm getting an error (Cannot use 'new' with an expression whose type lacks a call or construct signature) when trying to create a new instance of Swiper.

StackBlitz

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Liad Idan
  • 556
  • 1
  • 7
  • 17
  • I tried to reproduce the error on my local and while trying to do so, i first installed Swiper with 'npm i --save swiper' then Swiper types with 'npm install --save @types/swiper' and somehow, it uninstalled Swiper. I tried tsconfig and typings.d.ts modification also but nothing worked. Then i realized that Swiper is missing from my node_modules. Installed Swiper again and the error is gone :). Please check your node_modules one more time. – Learning Oct 05 '18 at 22:38
  • 1
    The problem occurs when I'm typing the variables and not related to Swiper, please check out the StackBlitz. btw, as of npm 5 modules are added as a dependency by default so the --save option is redundant (; – Liad Idan Oct 05 '18 at 22:52

1 Answers1

2

The declaration of the swiper module in @types/swiper is wrong: the declaration says that the module has a named export Swiper of the Swiper class, but it actually has a default export of the class (or an export assignment depending on whether your bundler uses the CommonJS or the ES6 version: argh, stupid packaging). I've submitted a pull request to DefinitelyTyped to attempt to fix the declaration. If you use my modified declaration (see this answer for the possible ways to do that), your code should compile successfully.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • When I wrote this type definition I did not think that someone would possibly want to use the non-esm version. – Machado Oct 08 '18 at 11:39