简体   繁体   中英

TS2307 error when including external modules to Typescript file

I installed a module via npm, and am trying to access it inside my typescript file.

npm install marker-animate-unobtrusive --save

import SlidingMarker = require('marker-animate-unobtrusive');

This results in

//Error TS2307: Cannot find module 'marker-animate-unobtrusive'

Search for this issue brings upchanging the compiler options, others mention creating a d.ts file for Type Script to recognize the module, but I never got a clear answer anywhere. I tried these methods, but with little success so far. I am using Angular 2 and Ionic 2 for this, if that information helps.

Any help is appreciated!!

The problem is because the SlidingMarker npm module doesn't yet have a type definition.

1) Create generic definition in typings/marker-animate-unobtrusive.d.ts:

declare module 'marker-animate-unobtrusive' {
  const x: any;
  export = x;
}

2) Add this file to list of definitions in typings/main.d.ts (or typings/index.d.ts if you are using the newer typings):

/// <reference path="marker-animate-unobtrusive.d.ts"></reference>

3) Next, update your import statement:

import * as SlidingMarker from 'marker-animate-unobtrusive';

Volia! Note, you may need to change any variables cast as "SlidingMarker" to "any" to avoid other TypeScript errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM