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
相关推荐
To_OC1 小时前
LC 15 三数之和:双指针不难,难的是把去重做对
javascript·算法·leetcode
触底反弹1 小时前
🔥 前端也能玩转 AI 流式输出!从二进制流到打字机效果,一篇讲透
javascript·人工智能·node.js
胡萝卜术1 小时前
深度重构:Agent Skills——从 Prompt 工程到能力工程
javascript·架构·github
To_OC4 小时前
从 “卡死半天” 到 “打字机效果”:大模型流式输出前端实现全记录
前端·javascript·llm
rockey6274 小时前
基于AScript的JavaScript脚本语言发布啦
javascript·c#·.net·js·script
kyriewen5 小时前
我看完这篇安全论文——AI推荐的npm包,92%是编出来的
前端·javascript·ai编程
心中有国也有家5 小时前
AtomGit Flutter 鸿蒙客户端:Canvas 绘制进阶-路径、渐变与混合模式
android·javascript·flutter·华为·harmonyos
fanchenxinok6 小时前
学习笔记:LabVIEW调用DLL解析S19文件教程
笔记·学习·labview·dll解析
疯狂打码的少年6 小时前
【软件工程】结构化设计:模块独立性与耦合内聚
java·开发语言·笔记·软件工程
鹿目7 小时前
使用 Vant CLI 搭建 UI 组件库:从 H5 到小程序跨平台
前端·javascript