一天都没写代码 wu wu…
先来几个练手
const numberArr :number[]=[1,2,3]
const stringArr:string[]=['a','b','c']
const undefineArr:undefined[]=[undefined,undefined]
const arr:(number|string)[]=[1,'string',2]
const xiaojiejies:{name:string,age:number}[]=[
{name:'貂蝉',age:16},
{name:'西施',age:18},
]
// type alis 类型别名
type lady={name:string,age:number}
const xiaojiejies1:lady[]=[
{name:'貂蝉',age:16},
{name:'西施',age:18},
]
class Mandam {
name:string;
age: number;
}
const xiaojiejies2:Mandam[]=[
{name:'貂蝉',age:16},
{name:'西施',age:18},
]
元组数组的使用
const xiaojiejie1:[string,string,number]=['貂蝉','法师',16]
const xiaojiejies3:[string,string,number][]=[
['貂蝉','法师',16],
['西施','法师',18],
['杨玉环','辅助',15]
]
interface
const jianli=(name:string,age:number,best:number)=>{
age<=24&&best>90&&console.log(name+"合格");
age>24||best<=90||console.log(name+"淘汰");
}
jianli("貂蝉",16,99);
运行结果
PS D:\Ts-demo> ts-node demo.ts
貂蝉合格
PS D:\Ts-demo>
使用接口
interface Girl{
name:string;
age:number;
bust:number;
}
const gril={
name:'貂蝉',
age :18,
bust:94
}
const jianli=(gril:Girl)=>{
gril.age<=24&&gril.bust>90&&console.log(gril.name+'进入面试')
gril.age>24||gril.bust<90&&console.log(gril.name+'被淘汰了')
}
const getResume=(gril:Girl)=>{
console.log(gril.name+'年龄是'+gril.age)
console.log(gril.name+'胸围是'+gril.bust)
}
jianli(gril)
getResume(gril)
结果
PS D:\Ts-demo> tsc demo1.ts
PS D:\Ts-demo> node demo1.js
貂蝉进入面试
貂蝉年龄是18
貂蝉胸围是94
PS D:\Ts-demo>