我是 TypeScript 的新手,我的 api 命名空间中有一个函数,位于 api.ts
文件中,用于连接到我的 api:
namespace api
{
export function connect() : boolean
{
return false;
}
}
这是我
test.ts
文件的内容。///<reference path="api.ts"/>
api.connect();
使用TypeScript编译后会生成两个文件:api.js
和test.js
。是否可以将其编译为一个名为test.js
的文件,但将otherPage.ts
编译为单独的包?
注意:
这是我的文件结构:
- test.ts
- api.ts
- otherPage.ts
这是期望的输出:
- test.js (api.ts and test.ts compiled into one file)
- otherPage.js
这是我用于自动编译文件的命令:
tsc --watch
。我希望有一种解决方案,可以避免我针对要包含的文件更改此命令。我在多个文件中使用
api.ts
,因此创建一个ts
文件对我来说不是一个选项。
临时解决方案:
作为临时解决方案,我将api.ts
包含在所有文件中。这对我来说不是大问题,因为我在将TypeScript编译为JavaScript后,使用Google Closure Compiler编译JavaScript文件。我使用Visual Studio Code,所以我可以在我的
tasks.json
中自动化此过程:{
"version": "0.1.0",
"command": "tsc",
"showOutput": "silent",
"windows": {
"command": "tsc.cmd"
},
"args":
[
"res/ts/modules/api.ts",
"${file}",
"--outFile", "res/js/${fileBasenameNoExtension}.js",
"--listFiles", "true"
],
"problemMatcher": "$tsc"
}
按下 ctrl + b 来执行它。
api.ts
。 - user7353781