我需要调用以下javascript:

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

return publicMethods;
})();


我是Typescript的新手,能否请您分享上述javascript的Typescript定义,并调用模式?

提前致谢。

最佳答案

我假设以下目录结构

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


jslib.js

var jslib = jslib || (function () {
  var publicMethods = {
    encript: function () {
      return
    }
  };

  return publicMethods;
})();

module.exports = jslib;


jslib.d.ts

declare namespace jslib {
  function encript(): void;
}

export = jslib;




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

jslib.encript();


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'. ========


jslib.jsjslib.d.ts放在同一目录中至关重要。

09-19 20:10