typescript笔记、ts笔记、npx命令

文章目录

    • 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
相关推荐
rockey62710 小时前
C#脚本引擎之AScript与Jurassic、Jint对比
javascript·c#·.net·js·script·动态脚本
xian_wwq10 小时前
【学习笔记】-深度认知系列-第2讲-大模型到底是什么?——拆解“参数、训练、推理”
笔记·学习·深度认知
lzhdim11 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
红尘散仙12 小时前
TypeScript WIT Guest:类型约束与 ComponentizeJS 工程化
rust·typescript·webassembly
Oll Correct13 小时前
Adobe illustrator 案例九:百宝箱图标绘制
笔记·ui·adobe·illustrator
红尘散仙13 小时前
从 dsh 的插件系统出发:为什么我要探索 WIT 与 Wasm Component Model
rust·typescript·webassembly
红尘散仙13 小时前
从 WIT 到 Wasmtime:构建 Rust Component 工程
rust·typescript·webassembly
M78佐菲14 小时前
Linux学习笔记:进程
linux·笔记·学习·算法
kyriewen15 小时前
我踩了3次同一个坑才明白:JavaScript里比0.1+0.2更隐蔽的5个数字陷阱
前端·javascript·面试
记忆张量MemTensor15 小时前
产品更新|MemOS 现已支持 DeepSeek Harness 长期记忆接入
人工智能·typescript·开源·agent