简体   繁体   English

如何使用es6语法在Typescript中导入功能模块

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

I have a simple module. 我有一个简单的模块。 Use for checking variable type. 用于检查变量类型。

index.js 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. typeOf函数出现未定义的错误。 Do I miss something? 我想念什么吗?

when you export node like with Javascript: 当您使用Javascript导出节点时:

module.exports = something;

in Typescript import it like : 在Typescript中将其导入为:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ES6:将模块作为功能导入 - ES6: Import a module as a function 如何将扩展导入ES6 / Typescript模块 - How do I import extensions to an ES6/Typescript module 如何使TypeScript输出有效的ES6模块导入语句? - How to make TypeScript output valid ES6 module import statements? 在ES6的`import`语法中,如何精确评估模块? - In the `import` syntax of ES6, how is a module evaluated exactly? 如何使用 ES6 模块语法(析构)导入属性? - How to import into properties using ES6 module syntax (destructing)? 有没有`从<module>进口<class> ` ES6 中的语法?</class></module> - is there `from <module> import <class>` syntax in ES6? 无法使用ES6导入语法 - Can't use ES6 import syntax 如何通过es6导入导入nodejs模块“module”以使用“createRequire”创建“require”函数以使用节点的“require”? - How to import nodejs module "module" via es6 import to create "require" function with "createRequire" in order to use node's "require"? 如何模拟使用在节点模块中导出的导入(ES6 打字稿)进行单元测试的外部注入库 - How do I mock externally injected libraries that use import (ES6 typescript) exported in a node module for unit testing 如何使用es6语法导入ReactCSSTransitionGroup? - How to import ReactCSSTransitionGroup using es6 syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM