1

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 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

9 Comments

I changed as you said. But the compiler said error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "E:/project/node_modules/lc-type-of/index"' has no compatible call signatures.
After adding your addition code. It works now! Awsome mate! Thank you.
Can I ask you why I have to declare a module ? It is just a function.
how would you import it if its not a module ? :)
You can actually just export the function @Dan! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.