1

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.

1 Answer 1

1

I'm assuming the following directory structure

├── 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; 

t.ts

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

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks zjk. I have two dumb questions 1) in javascript,how to understand var jslib = jslib || function(), I do not understand why jslib needs show up again after "=" 2) in your answer, what does const jslib mean? sorry, I am green to both scripts..
@SkyLine. Good questions! I'm no expert. I'll update in my answer
I have not tried my answer. I'll expand it later. As for var jslib = jslib || function(), it can prevent redefine the whole jslib if it is already defined.
Thanks @zjk. i am looking forward your update then :)
@SkyLine I must admit that I do not understand this issue well enough to answer this question. Sorry for the delay.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.