TypeScript: Prevent loading @types definition that is not exported (TS2306 is not a module)

I've been diving into trying to download a NodeJS library for hours now, which has (in my opinion wrong) type definition in the @types repository.

I'm talking about geolib and its @ types / geolib

Yes, I know the library has types on master, but there is no tagged release for it yet, and I want to allow that for future cases.

Installation geolib

(2.0.22 at the time)

  • npm install geolib --save

  • import * as geolib from 'geolib';

everything is amazing! but no type permission in your favorite editor

Installation @types/geolib

  • npm install @types/geolib --save-dev

Here's where it breaks down:

node_modules/@types/geolib/index.d.ts' is not a module. (2306)

This is because the file does not have types export = geolib

, but it tries to load that file as a module.

My goal is to just download the node library as such and use it @types/geolib

as type hints for development.

I tried several options for example. using the new directive types

, preventing automatic loading, setting types: []

in tsconfig, importing only with help import 'geolib'

, but nothing works around the problem that the definition lies in @types

and is treated as a module.

Edit

  • typescript@2.3.4

  • geolib@2.0.22

  • @types/geolib@2.0.2

+3


source to share


1 answer


There was the same problem.

What worked for me:

import * as GeoLib from 'geolib';

      



and then for example:

import * as GeoLib from 'geolib';
import PositionAsDecimal = GeoLib.PositionAsDecimal;

distance += GeoLib.getDistance(pointAPositionAsDecimal, pointBPositionAsDecimal, accuracyInMeters, precision);

      

0


source







All Articles