文章目录
-
- npx命令
-
-
- [npx tsc](#npx tsc)
- 编译前后的对比
-
- 编译前
- 编译后
- ts和js的区别?
- [报错 error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.](#报错 error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.)
-
typescript并不是一个新概念,只不过随着2026-03-31(说04-01也没问题)claude code的源码泄露,ts又火了一把。
简单理解:
1、ts是静态类型
2、需要先编译成js才能运行
3、属于js开发阶段的增强版
npx命令
npx是npm的升级版。
npx tsc
tsc(typescript compiler)(ts编译器)。
npx tsc是编译ts的命令。
bash
初始化(生成tsconfig.json配置文件):
npx tsc --init
编译指定ts文件:
npx tsc index.ts --ignoreConfig
编译ts文件(根据tsconfig.json):
npx tsc
编译后会根据.ts生成同名的js文件。
编译前后的对比
看效果一目了然,举个最简单的例子。
编译前
ts
// 定义一个方向枚举
enum Direction {
Up,
Down
}
// 打印一下"向上"
console.log(Direction.Up);
编译后
js
"use strict";
// 定义一个方向枚举
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 0] = "Up";
Direction[Direction["Down"] = 1] = "Down";
})(Direction || (Direction = {}));
// 打印一下"向上"
console.log(Direction.Up);
ts和js的区别?
| --- | js | ts |
|---|---|---|
| 类型系统 | 动态类型(运行时才知对错) | 静态类型(写代码时就能查错) |
| 运行方式 | 解释型(直接拿去运行) | 需要先编译成js才能运行 |
| 本质 | 最终执行的底层语言 | js的开发阶段增强版 |
报错 error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.
bash
错误命令:
npx tsc index.ts
正确命令:
npx tsc index.ts --ignoreConfig