Javascript 为什么这在TypeScript“export{add}from';中是不可能的/数学`常量数学={add}`

Javascript 为什么这在TypeScript“export{add}from';中是不可能的/数学`常量数学={add}`,javascript,typescript,Javascript,Typescript,假设我有一个名为math.ts // math.ts function add() {} export { add }; 现在,如果我创建一个文件并重新导出它,但在export语句下面使用它: export { add } from './math'; const math = { add }; // No value exists in scope for the shorthand property 'add'. Either declare one or provide an initi

假设我有一个名为
math.ts

// math.ts
function add() {}
export { add };
现在,如果我创建一个文件并重新导出它,但在export语句下面使用它:

export { add } from './math';
const math = { add }; // No value exists in scope for the shorthand property 'add'. Either declare one or provide an initializer.

这会导致一个错误,但我不明白为什么这是无效的


是否可以实现这一点?

export..from
语法不会创建绑定到当前范围内导入和导出值的变量。它所做的只是导出导入。它实际上不做任何其他事情。您需要导入它(从而创建一个可用的引用),然后在单独的行上导出它:

import { add } from './math';
export { add };
/// now you can use add

export..from
语法不会创建绑定到当前范围内导入和导出值的变量。它所做的只是导出导入。它实际上不做任何其他事情。您需要导入它(从而创建一个可用的引用),然后在单独的行上导出它:

import { add } from './math';
export { add };
/// now you can use add

尝试从“/math”导入{add}:)尝试从“/math”导入{add}:)