node-device-detector icon indicating copy to clipboard operation
node-device-detector copied to clipboard

import instead of require

Open MickL opened this issue 2 years ago • 4 comments

When importing in TypeScript:

import DeviceDetector from 'node-device-detector';

const detector = new DeviceDetector();

I get the error:

node_device_detector_1.default is not a constructor

I also tried:

import * as DeviceDetector from 'node-device-detector';

const detector = new DeviceDetector();

Which gives a TypeScript error:

TS2351: This expression is not constructable.   Type 'typeof import("node-device-detector")' has no construct signatures.

But the latter works when adding // @ts-ignore, so there must be something wrong with the typings?

MickL avatar Apr 11 '22 09:04 MickL

if delete files 'node-device-detector/index.d.ts' and 'node-device-detector/helper.d.ts'

import DeviceDetector = require('node-device-detector');
const detector = new DeviceDetector();

const userAgent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
const result = detector.detect(userAgent);
console.log('result parse', result);

then, everything works.

TypeScript is not my strong suit :horse: , but I'll see what I can do.

sanchezzzhak avatar Apr 11 '22 13:04 sanchezzzhak

Since project has only interfaces and types for TS, it is necessary to use // @ts-ignore for constructor class. I've also updated index.d.ts for clienthints

https://github.com/sanchezzzhak/node-device-detector/blob/master/docs/TYPE_SCRIPT.MD

sanchezzzhak avatar Apr 15 '22 21:04 sanchezzzhak

In my opinion it should never be necessary to use // @ts-ignore. Within a lot of projects it is also forbidden to use ts-ignore by eslint rules.

The problem is that your package comes with types that are not correct. The typings need to be corrected, please reopen this issue, maybe someone will make a PR some day.

MickL avatar Jun 09 '22 10:06 MickL

To fix this, need to completely redo all files in typescript. I'm not ready to support this TypeScript. (fully)

If someone knows how to solve it through the declaration of classes https://github.com/sanchezzzhak/node-device-detector/blob/884dab2c636d8ce161768b6fbba7081e5e2fe1db/index.d.ts#L5-L9, share in this topic.

sanchezzzhak avatar Jun 09 '22 10:06 sanchezzzhak