typescript 在打字稿“预期声明或声明”上导出函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41993445/
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-09 07:34:12  来源:igfitidea点击:

Exporting a function on typescript "declaration or statement expected"

typescript

提问by Joaquin Brandan

I realize this is really simple but typescript seems to have changed a lot in the last years and i just cant get this done with previous answers i found here on stack overflow.

我意识到这真的很简单,但是打字稿在过去几年中似乎发生了很大变化,我无法使用我在堆栈溢出时在此处找到的先前答案来完成此操作。

let myfunction = something that returns a function

export myfunction;

I get an error "declaration or statement expected"

我收到错误“预期的声明或声明”

How can i export a function from a really simple ts file to be able to use the function in another ts file?

如何从一个非常简单的 ts 文件中导出一个函数,以便能够在另一个 ts 文件中使用该函数?

回答by Joaquin Brandan

It seems that

看起来

let myfunction = something that returns a function
export {myfunction};

will do the trick.

会做的伎俩。

回答by Philip Bijker

Use

export default myfunction

if you only have this function to export from this file. Otherwise use

如果您只有此功能可以从此文件导出。否则使用

export { myfunction, <other exports> }

to export myfunctionalong with other types to export

出口myfunction以及其他类型的出口

回答by jrbedard

You can call a functionor instantiate a classfrom another file using modular top-level importand exportdeclarations.

您可以使用模块化的顶级和声明从另一个文件调用function或实例化 a 。classimportexport

file1.ts

文件1.ts

// This file is an external module because it contains a top-level 'export'
export function foo() {
    console.log('hello');
}
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();