如何将多个 Typescript 文件编译成一个文件?
在本文中,我们将学习如何将多个 Typescript 文件编译成一个文件。这里可以采用两种方法:
方法 1:将多个 Typescript 文件编译成一个 JavaScript 文件。我们只需使用以下语法:
句法:
tsc –out outputFile.js typeScriptFile1.ts typeScriptFile2.ts … typeScriptFilen.ts
解释:
- tsc:它代表 TypeScript 编译器,用于调用编译器以编译 TypeScript 文件。
- –out:它是一个 CLI(命令行界面)命令,用于连接 TypeScript 文件并将输出发送到单个 JS 文件。
- outputFile.js:保存 TypeScript 文件编译结果的 JavaScript 文件。
- typeScriptFile1, typeScriptFile2 ... typeScriptFilen:要编译的 TypeScript 文件。
示例:在这里,三个名为script.ts 、 index.ts和app.ts的 TypeScript 文件被编译为一个 JavaScript 文件output.js 。随后,使用以下 CLI 命令执行 JavaScript 文件:
node output.js
script.ts
const myArr = [1, 2, 3, 4, 5];
console.log("CONTENTS OF FILE 1");
for (let i = 0; i < myArr.length; i++) {
console.log(myArr[i]);
}
index.ts
const GFG = ["Geeks", "For", "Geeks"];
console.log("\nCONTENTS OF FILE 2");
for (let i = 0; i < GFG.length; i++) {
console.log(GFG[i]);
}
app.ts
const geeks = [true, false, 2.5, 5];
console.log("\nCONTENTS OF FILE 3");
for (let i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
output.js
var myArr = [1, 2, 3, 4, 5];
console.log("CONTENTS OF FILE 1");
for (var i = 0; i < myArr.length; i++) {
console.log(myArr[i]);
}
var GFG = ["Geeks", "For", "Geeks"];
console.log("\nCONTENTS OF FILE 2");
for (var i = 0; i < GFG.length; i++) {
console.log(GFG[i]);
}
var geeks = [true, false, 2.5, 5];
console.log("\nCONTENTS OF FILE 3");
for (var i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
file1.ts
let i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
file2.ts
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
file3.ts
const geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (let i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
output.ts
var i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
var geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (var i_1 = 0; i_1 < geeks.length; i_1++) {
console.log(geeks[i_1]);
}
output.js
var i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
var geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (var i_1 = 0; i_1 < geeks.length; i_1++) {
console.log(geeks[i_1]);
}
索引.ts
const GFG = ["Geeks", "For", "Geeks"];
console.log("\nCONTENTS OF FILE 2");
for (let i = 0; i < GFG.length; i++) {
console.log(GFG[i]);
}
应用程序.ts
const geeks = [true, false, 2.5, 5];
console.log("\nCONTENTS OF FILE 3");
for (let i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
生成的输出 JavaScript 文件“output.js”
输出.js
var myArr = [1, 2, 3, 4, 5];
console.log("CONTENTS OF FILE 1");
for (var i = 0; i < myArr.length; i++) {
console.log(myArr[i]);
}
var GFG = ["Geeks", "For", "Geeks"];
console.log("\nCONTENTS OF FILE 2");
for (var i = 0; i < GFG.length; i++) {
console.log(GFG[i]);
}
var geeks = [true, false, 2.5, 5];
console.log("\nCONTENTS OF FILE 3");
for (var i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
输出:
方法 2:将多个 Typescript 文件编译成一个 TypeScript 文件。我们使用以下语法:
句法:
tsc –out outputFile.ts typeScriptFile1.ts typeScriptFile2.ts … typeScriptFilen.ts
解释:
- tsc:它代表 TypeScript 编译器,用于调用编译器以编译 TypeScript 文件。
- –out:它是一个 CLI(命令行界面)命令,用于连接 TypeScript 文件并将输出发送到单个 TypeScript 文件
- outputFile.ts:保存 TypeScript 文件编译结果的 TypeScript 文件
- typeScriptFile1, typeScriptFile2 ... typeScriptFilen:要编译的 TypeScript 文件。
示例:在这里,三个名为file1.ts 、 file2.ts和file3.ts的 TypeScript 文件被编译为一个 TypeScript 文件output.ts 。之后,将生成的 TypeScript 文件编译为 JavaScript 文件 o utput.js ,然后使用以下 CLI 命令执行 JavaScript 文件:
tsc output.ts
node output.js
文件1.ts
let i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
文件2.ts
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
文件3.ts
const geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (let i = 0; i < geeks.length; i++) {
console.log(geeks[i]);
}
生成的输出 TypeScript 文件“output.ts”
输出.ts
var i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
var geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (var i_1 = 0; i_1 < geeks.length; i_1++) {
console.log(geeks[i_1]);
}
生成的输出 JavaScript 文件“output.js”
输出.js
var i = 1;
console.log("CONTENTS OF FILE 1");
while (i <= 5) {
console.log(i);
i++;
}
console.log("\nCONTENTS OF FILE 2");
console.log("GeeksForGeeks is a computer science portal for geeks.");
var geeks = [2, 4, 6, 8];
console.log("\nCONTENTS OF FILE 3");
for (var i_1 = 0; i_1 < geeks.length; i_1++) {
console.log(geeks[i_1]);
}
输出: