How do I import a library into node without a Typescript/TSD definition?

4.2k Views Asked by At

I'm trying to use a session helper called connect-session-knex which is obscure enough that it does not have a published typescript definition. So when I try to compile my typescript node project, I get the error,

error TS2307 Cannot find module 'connect-session-knex'

Is there a way to ignore TS for this module only? How do I import it without the TSD? I know knex has a tsd, but the wrapper does not. I'm asking this from a generic standpoint of what to do with libraries without type definitions.

For anyone looking: Compiling typescript when it does not have tsd. Missing tsd. Without tsd.

2

There are 2 best solutions below

0
On BEST ANSWER

error TS2307 Cannot find module 'connect-session-knex' Is there a way to ignore TS for this module only? How do I import it without the TSD?

Use var/require instead of import/require. i.e.

var csk = require('connect-session-knex');

Note you should have node.d.ts included for require to be declared.

Also : https://basarat.gitbooks.io/typescript/content/docs/node/nodejs.html

4
On

Another suggestion is to start you own .d.ts file as an empty definition file and export the module. Then if you want to get intellisense on the module you can add definitions to it.

e.g. connect-session-knex.d.ts:


// declare module
declare module "connect-session-knex" {

}