Michael Pell
Michael Pell

Reputation: 1476

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 calljsSHA in my test file, but that value is undefined.

How do I actually use the exported interface?

Upvotes: 0

Views: 1037

Answers (2)

artem
artem

Reputation: 51809

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';

Upvotes: 3

kimy82
kimy82

Reputation: 4505

If it is actually an interface it is only used for building the js, ins't it? Then you need something like /// <reference path="path to .d.ts" /> at the top of the file

Upvotes: -1

Related Questions