介绍
随着TypeScript成为JavaScript的超组,使用它意味着在V8引擎能够理解它们之前将TypeScript文件编译成纯JavaScript。你可以观察文件的变化并自动编译,但有时,你只是想运行你的脚本并获得结果。这就是 ts-node
进入的地方。 使用 ts-node
,你可以轻松跳过混乱并执行你的TypeScript脚本。
前提条件
要成功完成本教程,您将需要以下内容:
您可以通过遵循 如何安装 Node.js 和创建本地开发环境 教程
- 熟悉
npm
. 有关如何使用npm
了解更多信息,请阅读此 如何使用 Node.js 模块与 npm 和 package.json 教程 - 熟悉 TypeScript. 此 如何设置一个新的 TypeScript 项目 文章是一个很好的开始的地方
步骤1 - 开始
要开始,你需要安装typescript
和ts-node
:
1npm install typescript ts-node
由于ts-node
是你可以运行的可执行的,所以你的脚本中没有什么可以导入
或要求
的。
如果你还没有一个TypeScript项目,你可以使用这个脚本来测试ts-node
:
1[label Script: reptile.ts]
2class Reptile {
3 private reptiles: Array<string> = [
4 'Alligator',
5 'Crocodile',
6 'Chameleon',
7 'Komodo Dragon',
8 'Iguana',
9 'Salamander',
10 'Snake',
11 'Lizard',
12 'Python',
13 'Tortoise',
14 'Turtle',
15 ];
16
17 shuffle(): void {
18 for (let i = this.reptiles.length - 1; i > 0; i--) {
19 let j: number = Math.floor(Math.random() * (i + 1));
20 let temp: string = this.reptiles[i];
21 this.reptiles[i] = this.reptiles[j];
22 this.reptiles[j] = temp;
23 }
24 }
25
26 random(count: number = 1, allowDupes?: boolean): Array<string> {
27 let selected: Array<string> = [];
28
29 if (!allowDupes && count > this.reptiles.length) {
30 throw new Error(`Can't ensure no dupes for that count`);
31 }
32
33 for (let i: number = 0; i < count; i++) {
34 if (allowDupes) {
35 // Dupes are cool, so let's just pull random reptiles
36 selected.push(this.reptiles[
37 Math.floor(Math.random() * this.reptiles.length)
38 ]);
39 } else {
40 // Dupes are no go, shuffle the array and grab a few
41 this.shuffle();
42 selected = this.reptiles.slice(0, count);
43 }
44 }
45
46 return selected;
47 }
48}
49
50const reptile = new Reptile();
51console.log(`With Dupes: ${reptile.random(10, true)}`);
52console.log(`And Without: ${reptile.random(10)}`);
上面的脚本从一个数组中抽取随机值,并返回两种不同类型的爬行动物列表:一个有重复和一个没有。
确保你的脚本返回一个值,并将某些东西打印到控制台上。你会想看到你的代码的结果。有了你的TypeScript脚本,你现在可以继续运行你的脚本。
步骤2 - 运行脚本
在使用ts-node 之前,最好知道在使用 Node 运行 TypeScript 脚本时会发生什么。
您将使用节点
命令运行reptile.ts
脚本(或您自己的TypeScript脚本):
1node reptile.ts
运行此命令后,您将看到一个错误消息:
1[secondary_label Output]
2SyntaxError: Unexpected identifier
SyntaxError: 意想不到的标识符
消息特别指出reptile.ts
的第二行上的私人类变量。
使用 Node 运行 TypeScript 脚本会返回错误. 现在你知道在执行时不应该做什么以及期望做什么。
使用ts-node
来运行reptile.ts
脚本:
1npx ts-node reptile.ts
如果您想了解更多关于 npx 命令的信息,这是一种工具,现在配备了 npm,允许您从命令行运行本地项目的二进制。
您将看到运行此脚本的此输出:
1[secondary_label Output]
2With Dupes: Komodo Dragon,Python,Tortoise,Iguana,Turtle,Salamander,Python,Python,Salamander,Snake
3And Without: Alligator,Iguana,Lizard,Snake,Tortoise,Chameleon,Crocodile,Komodo Dragon,Turtle,Salamander
使用ts-node
运行reptile.ts
将返回两个爬行动物类型列表,一个可能具有重复和一个没有。
步骤3 - 加快事情
在帽子下,ts-node
接收你的脚本,做一些语义检查,以确保你的代码没有错误,然后将你的TypeScript编译成JavaScript。
这是最安全的选择,但如果你不担心TypeScript错误,你可以通过-T
或--transpileOnly
旗帜。
如果你试图运行别人的脚本,或者如果你相信你的编辑器和发明器正在捕捉一切,使用-T
或--transpileOnly
旗是合适的。
1npx ts-node -T reptile.ts
运行此命令将为您提供与「npx ts-node reptile.ts」相同的输出。
步骤 4 – 使用TypeScript REPL
ts-node
的另一个额外的奖励是能够使用类似于运行node
的TypeScript REPL(读取评估打印循环),而无需任何选项。
这个TypeScript REPL允许您在命令行上直接写TypeScript,并且非常方便快速测试一些东西。
若要访问 TypeScript REPL,请在没有任何参数的情况下运行 `ts-node:
1npx ts-node
现在你可以享受TypeScript提供的所有严格性,就在你最喜欢的终端!
结论
在本文中,您使用ts-node
来运行 TypeScript 脚本,您还使用了ts-node
的 TypeScript REPL。
如果您有兴趣继续使用 TypeScript,您可能会发现这篇文章(How To Work With TypeScript in Visual Studio Code)(https://andsky.com/tech/tutorials/how-to-work-with-typescript-in-visual-studio-code)有用。