是否可以从typescript模块导出简单函数?

This isn't compiling for me.

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

This workitem似乎意味着你不能,但并不是直截了当地说出来.不可能吗?

推荐答案

在那个例子中,很难说你想要什么.exports =是关于从external个模块导出,但您链接的代码示例是internal个模块.

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

以下几点是有意义的(每个场景由一条横向规则描述):


// 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

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

export class bar { }

file2.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

// 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

// 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

Typescript相关问答推荐

如何在类型脚本中使用条件陈述循环

无需刷新即可让现场访客逆变

如何基于对象关键字派生类型?

使用FormArray在Angular中添加排序

有没有可能使用redux工具包的中间件同时监听状态的变化和操作

角形标题大小写所有单词除外

ANGLE找不到辅助‘路由出口’的路由路径

记录键的泛型类型

部分更新类型的类型相等

使用Redux Saga操作通道对操作进行排序不起作用

如何通过TypeScrip中的函数防止映射类型中未能通过复杂推理的any值

为什么我的动画状态在组件实例之间共享?

判断映射类型内的键值的条件类型

如何通过属性名在两个泛型数组中找到匹配的对象?

有没有办法传递泛型类型数组,以便推断每个元素的泛型类型?

从以下内容之一提取属性类型

如何在Next.js和Tailwind.css中更改悬停时的字体 colored颜色

Vite+Chrome扩展 list v3-;不能在模块外使用import语句;对于inpage脚本

React-跨组件共享功能的最佳实践

如何使用 runInInjectionContext 中的参数测试功能性路由防护