2016-12-22 30 views
1

私は単純なモジュールを持っています。変数型のチェックに使用します。es6構文を使用してTypescriptで汎用モジュールをインポートする方法

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; 

ここで私はそれを使用する方法について説明します。

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

しかし、コードは期待どおりに動作しません。 typeOf関数が定義されていないエラーを出しました。私は何かが恋しいですか?それが好きな活字輸入で

module.exports = something; 

:JavaScriptを使用してのようなエクスポートノード

答えて

2

import * as something from "./something" 

と定義

// 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; 
+0

をあなたが言ったように私が変更されました。しかし、コンパイラは、 '' 'エラーTS2349:型にコールシグネチャがない式を呼び出すことはできません。タイプ 'typeof "E:/ project/node_modules/lc-type-of/index"'に互換性のあるコールシグネチャがありません。 '' – JamesYin

+0

追加コードを追加した後。それは今働きます!仲間よ!ありがとうございました。 – JamesYin

+0

モジュールを宣言しなければならない理由は何ですか?それは単なる関数です。 – JamesYin

関連する問題