简体   繁体   English

从Typescript声明文件中的模块导出默认值

[英]Exporting default value from a module in Typescript declaration file

Let's say I have a declarations file foo.d.ts : 假设我有一个声明文件foo.d.ts

declare namespace foo {
  interface Bar {
    (): void;
  }
}

declare var foo: foo.Bar;
export default foo;

If I compile this: 如果我编译此:

import Foo from './foo';
Foo();

The resulting output is: 结果输出为:

"use strict";
var foo_1 = require('./foo');
foo_1["default"]();

However this code won't run as foo_1 is a function and has no property default . 但是,由于foo_1是一个函数且没有default属性,因此该代码无法运行。 How can I get the output to be foo_1() instead of foo_1["default"]() ? 如何获取输出为foo_1()而不是foo_1["default"]()

Use 采用

export = foo;

instead of export default foo; 而不是export default foo; in your declaration file 在您的申报文件中

and use import require when importing: 并在导入时使用import require

import Foo = require('./foo');

Export assignment/import require is special syntax in typescript for dealing with node modules with exports like 导出分配/导入要求是打字稿中的特殊语法,用于处理带有导出之类的节点模块

module.exports = function someFunction() {}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM