typescript 打字稿可以导出函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15651510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:02:13  来源:igfitidea点击:

Can typescript export a function?

syntaxtypescript

提问by George Mauer

Is it possible to export a simple function from a typescript module?

是否可以从打字稿模块导出一个简单的函数?

This isn't compiling for me.

这不是为我编译。

module SayHi {
    export function() {
    console.log("Hi");
  }
}
new SayHi();

This workitemseems to imply that you cannot but doesn't flat out say it. Is it not possible?

这个工作项似乎暗示你不能但没有直接说出来。不可能吗?

回答by Ryan Cavanaugh

It's hard to tell what you're going for in that example. exports =is about exporting from externalmodules, but the code sample you linked is an internalmodule.

在那个例子中很难说你要做什么。exports =是关于从外部模块导出,但您链接的代码示例是内部模块。

Rule of thumb: If you write module foo { ... }, you're writing an internal module; if you write export something somethingat top-level in a file, you're writing an external module. It's somewhat rare that you'd actually write export module fooat top-level (since then you'd be double-nesting the name), and it's even rarer that you'd write module fooin a file that had a top-level export (since foowould not be externally visible).

经验法则:如果你编写module foo { ... },你正在编写一个内部模块;如果您export something something在文件的顶层编写,则您正在编写一个外部模块。您实际上export module foo在顶级写入的情况很少见(从那时起您将双重嵌套名称),而且您module foo在具有顶级导出的文件中写入的情况更加罕见(因为foo不会外部可见)。

The following things make sense (each scenario delineated by a horizontal rule):

以下事情是有意义的(每个场景都由水平规则描绘):



// An internal module named SayHi with an exported function 'foo'
module SayHi {
    export function foo() {
       console.log("Hi");
    }

    export class bar { }
}

// N.B. this line could be in another file that has a
// <reference> tag to the file that has 'module SayHi' in it
SayHi.foo();
var b = new SayHi.bar();


file1.ts

文件1.ts

// This *file* is an external module because it has a top-level 'export'
export function foo() {
    console.log('hi');
}

export class bar { }

file2.ts

文件2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();


file1.ts

文件1.ts

// This will only work in 0.9.0+. This file is an external
// module because it has a top-level 'export'
function f() { }
function g() { }
export = { alpha: f, beta: g };

file2.ts

文件2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = require('file1');
f1.alpha(); // invokes f
f1.beta(); // invokes g

回答by Ray Hulha

To answer the title of your question directly because this comes up in Google first:

要直接回答您的问题的标题,因为这首先出现在 Google 中:

YES, TypeScript can export a function!

是的,TypeScript 可以导出函数!

Here is a direct quote from the TS Documentation:

这是 TS 文档的直接引用:

"Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword."

“任何声明(例如变量、函数、类、类型别名或接口)都可以通过添加 export 关键字导出。”

Reference Link

参考链接

回答by Eli

If you are using this for Angular, then export a function via a named export. Such as:

如果您将此用于 Angular,则通过命名导出导出函数。如:

function someFunc(){}

export { someFunc as someFuncName }

otherwise, Angular will complain that object is not a function.

否则,Angular 会抱怨 object 不是函数。

回答by Eli

In my case I'm doing it like this:

就我而言,我是这样做的:

 module SayHi {
    export default () => { console.log("Hi"); }
 }
 new SayHi();