TypeScript
https://www.typescriptlang.org/docs/handbook/2/basic-types.html
typescript在传统es的基础上增加了很多新特性。
1 tsc-typescript的编译器
因为目前主流的浏览器主要支持的脚本是es3,我们编写的ts代码需要首先编译为es3才能够被浏览器执行。tsc是专门编译ts的编译器
我们举几个例子说明ts的基础知识
function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } //greet("Brendan");
执行 tsc hello.ts ,编译会报错,但是还是在当前路径下生成了hello.js
function greet(person, date) { console.log(`Hello ${person}, today is ${date}!`); } greet("Brendan");
这种情况是ts编译器默认的行为,因为有时我们希望将js转换成ts,而如果编译出错代码就不能执行的话,这样看起来不合理。
如果确实要进行严格的语法检查,我么可以这样运行
tsc --noEmitOnError hello.ts
显式类型
function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); } greet("Maddison", new Date());
我们显式指定变量的类型,如果调用函数时类型不匹配,将会编译报错。
一般情况下,我们没必要显式指定类型,ts会自动判断类型
ts默认编译的目标版本为ES3。
function greet(person: string, date: Date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); } greet("Maddison", new Date());
比如上面的模板字符串``,经过编译后为
function greet(person, date) { console.log("Hello " + person + ", today is " + date.toDateString() + "!"); } greet("Maddison", new Date());
假如我们就是想编译为更高级别的es版本,使用 --target es2015 参数
ECMAScript2015(es2015或者es6)
tsc --target es2015 hello.ts
编译之后的文件
function greet(person, date) { console.log(`Hello ${person}, today is ${date.toDateString()}!`); } greet("Maddison", new Date());
持续更新....
https://blog.csdn.net/w96098/article/details/108326431
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异