Writing a typescript declaration file for an external js nodejs package without type info

0 votes

I'm writing a node js typescript library (for use in cli app context) which has a dependency on a js library jinxed, which has no typescript type declarations. I have been following information at Typescript declaration files

jinxed is a simple library that currently exports a single function via module.exports { ... }, it is not intended to be the sole export from the library, (since it will be expanded in the future). So the exported function is a member of the export object.

The problem is how do I consume this within a typescript library. After reading all the typescript declaration documentation, this is still not clear to me how to do this.

The first problem to solve is which declaration template to pick from Typescript declaration file templates.

I decided that the appropriate template to use to enable consuming jinxed from typescript is module.d.ts (I stand to be corrected though).

So my client typescript library (widget) looks like this:

widget/
  lib/
    externalTypes/
      jinxed/
        index.d.ts
    index.ts

So the above shows me defining a type file for the external dependency jinxed according to the instructions in the template file; ie create a folder whose name matches the name of the external dependency and place an index.d.ts file inside it.

widget/lib/externalTypes/jinxed/index.d.ts:

export function functify(obj: any): string;

On the consuming side; ie inside my index.ts file (widget/lib/index.ts):

/// <reference types="./externalTypes/jinxed" />

import jinxed = require('jinxed');

const TestObject:any = {
  name: 'dvorak',
  occ: 'watchman'
};

jinxed.functify(TestObject);

[PS: I have also tried:

let jinxed = require('./externalTypes/jinxed'); ( ... but this just results in an error about using the require statment)

]

But when compiled results in the following error message:

ERROR in /Users/User/dev/github/js/widget/lib/index.ts ./lib/index.ts [tsl] ERROR in

/Users/User/dev/github/js/widget/lib/index.ts(4,25) TS7016: Could not find a declaration file for module 'jinxed'. '/Users/User/dev/github/js/widget/node_modules/jinxed/index.js' implicitly has an 'any' type.

(Apologies for the error message looking a bit ugly, formatting issues)

[

And for the sake of completeness, the content of jinxed is as follows:

'use strict';

function functify (obj) {
  return JSON.stringify(obj, (key, val) => {
    return (typeof val === 'function') ? '_function_' : val;
  });
}

module.exports = {
  functify: functify
}

]

The error message is indicating that it can't find the declaration file for jinxed, but I have used the

 /// <reference...

to specify the type file to use, so I don't know what the problem is.

The dependencies in widget packaage.json is as follows:

  "dependencies": {
    "@types/ramda": "^0.26.19",
    "jinxed": "0.0.1",
    "ramda": "^0.26.1",
    "xpath": "0.0.27"   },

The tsconfig file is:

{
  "compilerOptions": {
    "allowJs": true,
    "alwaysStrict": true,
    "module": "commonjs",
    "moduleResolution": "Node",
    "noImplicitAny": true,
    "sourceMap": true,
    "strictNullChecks": true,
    "target": "es5",
    "types": [],
    "lib": [
      "es5",
      "es2015",
      "es6",
      "dom"
    ]
  },
  "include": [
    "lib/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

The "main" entry in package.json is:

"main": "./lib/index.ts",

Also, I would like to know if the way I have structured external type dependencies seems reasonable. IE, let's say, I am trying to consume another external js library in a similar way to that illustrated above for jinxed; I would assume, that I would create another folder under externalTypes that matches the name of that dependency and populate it with its own index.d.ts. Is that correct or am I barking up the wrong tree?

EDIT: (I found the solution to this problem via this issue, but I don't really userstand this.

In the index.d.ts, I have updated it to be as follows:

declare module 'jinxed' {
  export function functify(obj: any): string;
}

... and this does actually work.

However, I don't understand the declare module statement, in particular, why the names jinxed is inside quotes (remove the quotes and you get an error: TS7016: Could not find a declaration file for module 'jinxed'). So far I have not found the correct typescript template that this corresponds to, so I can't get a good explanation of why this works.

Aug 19, 2022 in Node-js by Neha
• 9,020 points
3,467 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
1 answer

How to download a file with Node.js without using third-party libraries?

Hii, You can create an HTTP GET request and pipe ...READ MORE

answered Nov 24, 2020 in Node-js by Niroj
• 82,840 points
1,270 views
0 votes
1 answer

Can I display a TV channel from TV tuner card using NodeJS for an Electron APP?

Tvheadend is a streaming server for live ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
780 views
0 votes
1 answer

Error:“NODE_ENV” is not recognized as an internal or external command, operable command or batch file

Hello @kartik, It sounds like your error comes ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
20,248 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

Hello @kartik, I have created the following, and ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
41,318 views
0 votes
1 answer

How to execute an external program from within Node.js?

Hello @kartik, Exec has memory limitation of buffer ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
4,068 views
0 votes
1 answer

How to create an Excel File with Nodejs?

Hello @kartik, Just create a file with Tabs ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
2,469 views
0 votes
1 answer

How to install and run Typescript locally in npm?

It took me a while to figure ...READ MORE

answered Jun 8, 2022 in TypeSript by Nina
• 3,060 points
4,262 views
0 votes
1 answer
0 votes
1 answer

How can I define a type for a CSS color in TypeScript?

There was a proposal for a type of ...READ MORE

answered Jun 15, 2022 in TypeSript by Nina
• 3,060 points
4,210 views
0 votes
0 answers

What is a type in Typescript for the Component class in Angular 2+?

I have a small problem, but big ...READ MORE

Jul 5, 2022 in TypeSript by Logan
• 2,140 points
739 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP