ts是js的超集,在名称上看,是type类型,是在js的基础之上增加了类型定义。
ts声明变量有两种形式,直接声明和对象声明
直接声明
const count:number=1
var uname:string = "Runoob";
var score1:number = 50;
var score2:number = 42.50
var sum = score1 + score2
console.log("名字: "+uname)
console.log("第一个科目成绩: "+score1)
console.log("第二个科目成绩: "+score2)
console.log("总成绩: "+sum)
利用interface声明(类似于对象)
interface fly {
age:number
}
interface ant {
name:string
}
const flyant : fly & ant={
name:"飞翔的蚂蚁",
age:18
}
console.log(flyant);
interface限制传入的属性名和类型,然后调用时直接将这个当类型直接写入
也可以用type声明,两者几乎没什么区别,我的建议时尽量interface,没有办法就用tpye。
type fly ={
age:number
}
type ant= {
name:string
}
const flyant : fly & ant={
name:"飞翔的蚂蚁",
age:18
}
console.log(flyant);