【问题标题】:Typescript compile with importsTypescript 使用导入编译
【发布时间】:2020-08-07 14:35:31
【问题描述】:

所以我有两个文件:a.tsb.tsb 导出一些东西供a 使用。 TypeScript 编译器可以很好地处理这个问题,但拒绝为浏览器创建有效的输出。示例:

import { test } from "./b";

console.log(test);

b.ts

export const test = "quest";

现在我尝试编译它:

$ tsc --lib dom,es2018 --target ES2018 --out test.js a.ts b.ts
b.ts:1:1 - error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is 'amd' or 'system'.

1 export const test = "quest";
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

嗯,奇怪。好的,让我们尝试使用--module。如果我使用amd,加载test.js 的浏览器会告诉我:ReferenceError: define is not defined。如果我尝试system,它会说:ReferenceError: System is not defined

好的,这不起作用。让我们尝试不使用--out 设置单个输出文件。这给了我:

SyntaxError: import declarations may only appear at top level of a module a.js:1
SyntaxError: export declarations may only appear at top level of a module b.js:1

因此,基于此,我使用 typescript 的唯一选择似乎是:

  • 将所有代码放在一个文件中
  • 使用像 require.js 或 webpack 这样的外部库
  • 了解什么是模块以及是否/如何在浏览器中使用它

或者我错过了什么?如何在不添加库/模块的情况下使用导入编译 Typescript?

TypeScript 版本:3.8.3

【问题讨论】:

  • 你有没有尝试过声明变量,然后导出它? const test = ""; export test;
  • 导出行然后给我“预期的声明或声明”作为编译错误

标签: javascript typescript


【解决方案1】:

语法错误

请改正

export const test = "quest";

export test = "quest";

导出时无需在此处添加任何 const

【讨论】:

  • 不,我现在所做的没有任何编译器错误。如果我按照您的建议进行操作,则会收到编译器错误“预期声明或声明”。
  • module.exports = 试试这个
  • Cannot find name 'module'
猜你喜欢
  • 2020-10-23
  • 2017-06-17
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多