typescript的简介

简介

tsc

shell 复制代码
npm install -g typescript
shell 复制代码
tsc -v

作用

检查类型和语法错误,提前纠错

ts的类型

如何穿件带有ts的vue工程

作用

常见类型

用法

编写一个ts文件

js 复制代码
let username:string = "John";

let age:number = 25;

let isUpdated:boolean = true;

let data:any = "Hello World!";

console.log(username);
console.log(age);
console.log(isUpdated);
console.log(data);

编译

shell 复制代码
 tsc .\tsDemo.ts

效果

对应的ts文件下多了一个js文件

  • ts文件是不能直接运行的,只有编译成js才能运行
    这是编译之后的js文件
js 复制代码
var username = "John";
var age = 25;
var isUpdated = true;
var data = "Hello World!";
console.log(username);
console.log(age);
console.log(isUpdated);
console.log(data);

约束字面量类型(类似于enum枚举)

js 复制代码
myPrintFunction("Hello World!","center");
myPrintFunction("Hello World!","left");
myPrintFunction("Hello World!","right");

function myPrintFunction(content:string,alignment:'right'|'left'|'center'):void {
    console.log(content,alignment);
}
相关推荐
子兮曰13 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖13 小时前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神13 小时前
github发布pages的几种状态记录
前端
不像程序员的程序媛15 小时前
Nginx日志切分
服务器·前端·nginx
Daniel李华15 小时前
echarts使用案例
android·javascript·echarts
北原_春希15 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
JY-HPS15 小时前
echarts天气折线图
javascript·vue.js·echarts
尽意啊15 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜15 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive15 小时前
Vue3使用ECharts
前端·javascript·echarts