簡體   English   中英

如何使用es6語法在Typescript中導入功能模塊

[英]How to use es6 syntax to import a function module in Typescript

我有一個簡單的模塊。 用於檢查變量類型。

index.js

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

索引

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

在這里我如何使用它。

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

但是代碼無法按預期工作。 typeOf函數出現未定義的錯誤。 我想念什么嗎?

當您使用Javascript導出節點時:

module.exports = something;

在Typescript中將其導入為:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM