How to use es6 syntax to import a function module in Typescript

1.7k views Asked by At

I have a simple module. Use for checking variable type.

index.js

'use strict';
var typeOf = function (variable) {
    return ({}).toString.call(variable).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};
module.exports = typeOf;

index.d.ts

export default typeOf;
declare function typeOf(value:any):string;

Here how I use it.

import typeOf from 'lc-type-of';
typeOf(value);

But the code dosen't work as expect. The typeOf function came out undefined error. Do I miss something?

1

There are 1 answers

9
Dan On BEST ANSWER

when you export node like with Javascript:

module.exports = something;

in Typescript import it like :

import * as something from "./something"

and in the definition

// Tell typescript about the signature of the function you want to export
declare const something: ()=> void ;

// tell typescript how to import it     
declare module something {
      // Module does Nothing , it simply tells about it's existence
}

// Export 
export =  something;