Michael Pell Michael Pell - 9 days ago 9
TypeScript Question

How to use exported interface from TypeScript definition file?

I am trying to use the typed version of

jssha
and have run
npm install @types/jssha --save
and installed the definitions (as well as the library itself).

In my test file I now have
import { jsSHA } from 'jssha'
and in the @types/jssha index.d.ts I see
export interface jsSHA {...}


I would expect to be able to now call
jsSHA
in my test file, but that value is
undefined
.

How do I actually use the exported interface?

Answer

export interface jsSHA {...} is not an indicator how you should import it, because it's declared inside declare namespace jsSHA {.

When you look at toplevel exports in that definition file (at the very end), you see

declare var jsSHA: jsSHA.jsSHA;
export = jsSHA;
export as namespace jsSHA;

Whenever you see export =, the best way to import is via import require:

import jsSHA = require('jssha');

var shaObj = new jsSHA("SHA-512", "TEXT");
shaObj.update("This is a test");
var hash = shaObj.getHash("HEX");

If you are compiling with module=commonjs, this import will work too:

import * as jsSHA from 'jssha';