住房和城乡建设部网站电话,网站建设费用 优帮云,制作网页常用图片格式,wordpress万能主题一.安装ts
1.命令行运行如下命令#xff0c;全局安装 TypeScript#xff1a;
npm install -g typescript2.安装完成后#xff0c;在控制台运行如下命令#xff0c;检查安装是否成功
tsc -V 二.创建ts文件
1.创建一个day01.ts文件#xff0c;随便在里面码一点代码…一.安装ts
1.命令行运行如下命令全局安装 TypeScript
npm install -g typescript2.安装完成后在控制台运行如下命令检查安装是否成功
tsc -V 二.创建ts文件
1.创建一个day01.ts文件随便在里面码一点代码
console.log(hello ts)
2.在命令行上运行 TypeScript 编译器手动编译为js文件
tsc day01.ts输出结果为一个 day01.js 文件它包含了和输入文件中相同的 JavsScript 代码。
3.在命令行上通过 Node.js 运行这段代码
node day01.js
控制台会打印出来
hello ts
4.vscode自动编译ts文件
1. 生成配置文件tsconfig.jsontsc --init
2. 修改tsconfig.json配置outDir: ./js, //输出文件到当前文件下的js文件夹strict: false,
3. 启动监视任务: 终端 - 运行任务 - 监视tsconfig.json三.基本类型的定义规范
1.基础语法
首先在 TS 中创建变量时var、let、const 还是和 JS 中的使用方式一样但是在声明变量时需要约束变量的数据类型
var 变量名: 类型 值;2.基本定义示例
var a:number 1
var b:string hello
var c:boolean true
var d:undefined undefined
var e:null null
3.void
void 在 TS 中用来表示没有任何数据类型。通常当一个函数没有返回值的时候我们会将返回值的类型设置为 void如果return了一个返回值会报错。
例如定义一个函数没有返回值
function f(): void {console.log(123)
}
4.any
any 在 TS 中用来表示任意数据类型。通常只有当值来自于一些动态的内容我们在一开始确定不了值的类型可以选择使用 any。其他情况下都不推荐使用 any
var g: any hello
g 123
g null
5.array
TS 中要求数组中的每一项必须是同一个数据类型。
var h: number[] [1, 2, 3]
var i: Arraynumber [1,2,3] //定义的数组里只能存在number类型的数据//Arraystring [1,2,3] //只能存在string类型的数据
var j: Arrayany [1, 2, true] //如果存在不同类型推荐使用 any
6.元组
元组中允许一个数组中保存多个类型的数据。
但是要求数组中的值与元组类型必须数量要对应、位置要对应、类型要对应。
var k: [number, string] [1, hello]
7.object TS 中定义对象时需要定义出对象中有哪些属性每一个属性的值是什么类型。
var l: { id: number, name: string } {id: 1,name: 逍遥的码农
}
8.enum枚举
enum num {one,two,three
}
var m:num num.one
console.log(m) //0
这里会输出0因为枚举不赋值会取下标
赋值情况下看一下
enum num {one,two 5,three
}
var m:num num.three
console.log(m) //6
这里会输出6这里会递增
1普通枚举编译js会生成对应的代码
//ts代码
enum num {one,two 5,three
}
var m:num num.three
console.log(m)//js代码
// var num;
// (function (num) {
// num[num[one] 0] one;
// num[num[two] 5] two;
// num[num[three] 6] three;
// })(num || (num {}));
// var m num.three;
// console.log(m);
2常量枚举编译后不会产生多余的代码
//ts代码
const enum num {one,two 5,three
}
var m:num num.three
console.log(m)//js代码
// var m 6 /* num.three */;
可以看到就编译了一行
9.never
never类型表示永远不会有值的一种类型。
// 返回never的函数必须存在无法达到的终点
// 因为总是抛出异常所以 error 将不会有返回值
function error(message: string): never {throw new Error(message);
}
// 因为存在死循环所以 infiniteLoop 将不会有返回值
function infiniteLoop(): never {while (true) {}
}
文章转载自: http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.nlpbh.cn.gov.cn.nlpbh.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn