1

i am using express with typescript. there i am importing a module

import SomeModuleType from '3rd-party-module';

but since this didn't had type definitions available in npm

src/app.ts:5:21 - error TS7016: Could not find a declaration file for module '3rd-party-module'. 'C:/Work/node_modules/3rd-party-module/dist/index.js' implicitly has an 'any' type. Try 'npm i --save-dev @types/3rd-party-module' if it exists or add a new declaration (.d.ts) file containing 'declare module '3rd-party-module'; 5 import SomeModuleType from "3rd-party-module";

so used method given here. the error was gone but, but now the import was unable to find the module definitions from node_modules.

const logger = new SomeModuleType.ContentLogger();

gave error TypeError: Cannot read property 'ContentLogger' of undefined

i tried Go to definition in vsCode, but that was taking me to the custom index.d.ts file containing

declare module "3rd-party-module";

how to make this run. with proper typescript rules.

Shubham Shaw
  • 841
  • 1
  • 11
  • 24
  • Could you specify which module you're trying to import (and which libraery it comes from)? The syntax for importing varies depending on how the module is exported. – Searnd Aug 26 '21 at 13:33
  • actually its a private package of my client. so, it's unavailable publicly and also it does not have @types packages.. the main issue of confusion is, when imported in app.js file, the ctrl+click is working, but when i am importing in the app.ts file, the import doesn't work – Shubham Shaw Aug 26 '21 at 13:44
  • Oh, I see. I've added a couple of possible syntaxes that could maybe do the trick in my answer below – Searnd Aug 26 '21 at 13:54

1 Answers1

0

Try importing using one of the following syntaxes:

import * as SomeModuleType from '3rd-party-module';
import SomeModuleType = require('3rd-party-module');

Last resort:

const SomeModuleType = require('3rd-party-module');
Searnd
  • 91
  • 3