Sky Line Sky Line - 1 year ago 94
Javascript Question

how to write typescript definition file for this javascript library?

I need call the following javascript:

var jslib = jslib || (function() {
var publicMethods = {
encrypt: function (algorithm, keyHandle, buffer) {
// implementation

return publicMethods;

I am new to typescript, can you please share what the typescript definition will look like for the javascript above and invoke pattern?

Thanks in advance.

zjk zjk
Answer Source

I'm assuming the following directory structure

├── lib
│   ├── jslib.d.ts
│   └── jslib.js
├── src
    └── t.ts


var jslib = jslib || (function () {
  var publicMethods = {
    encript: function () {

  return publicMethods;

module.exports = jslib;


declare namespace jslib {
  function encript(): void;

export = jslib;


import jslib = require('../lib/jslib');


Check with node_modules/.bin/tsc --traceResolution

======== Resolving module '../lib/jslib' from '/home/zjk/dev/webnote/ts1/src/t.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module as file / folder, candidate module location '/home/zjk/dev/webnote/ts1/lib/jslib'.
File '/home/zjk/dev/webnote/ts1/lib/jslib.ts' does not exist.
File '/home/zjk/dev/webnote/ts1/lib/jslib.tsx' does not exist.
File '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts' exist - use it as a name resolution result.
Resolving real path for '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts', result '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts'
======== Module name '../lib/jslib' was successfully resolved to '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts'. ========

It is crucial to put jslib.js and jslib.d.ts in the same directory.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download