我可以在 {...} 中导入 ES5 导出的函数吗

Can I import function that exported by ES5 in the {...}

我有一个 ES5 模块可以导出像这样的简单函数

var sum = function(a, b) {
  return a + b;
}

module.exports = sum;
module.exports.default = sum;

我有一个导入 sum 函数的打字稿模块,但它只有在我将它导入到 {...} 之外或者如果我想将它放在 {... } 像这样

// This works
import sum from './sum';

// This works too
// import { default as sum } from './sum'; 

// This doesn't work (I want this to work but don't know how)
// import { sum } from './sum'; 

console.log(sum(1, 2));

有什么办法(也许更改 ES5 模块)让我可以像 { sum } 一样导入它并且它会起作用吗?欢迎任何建议。

查看 Stackblitz 中的代码:https://stackblitz.com/edit/import-es5-export-function-using-brackets

您只需像这样使用 module.exports

module.exports = {sum}

here it is working