JamesYin
JamesYin

Reputation: 742

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

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?

Upvotes: 1

Views: 1748

Answers (1)

Dan
Dan

Reputation: 2858

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;

Upvotes: 2

Related Questions