类型注释
‘:’ + 注释类型
//参数
function greeter(person:string) {
return 'Hello' + person
}
//变量
let count: Number = 1;
//自动推断
let text = 'string'; //推断为 let text: string = 'string'
text = 0; //报错
//联合类型
let age: string | number;
age = '18';
age = 18;
接口
interface定义接口
interface GreeterConf{
name: String,
age?: Number, //?表示参选
readonly label: String //readonly 表示创建后不可修改
}
let greeter: GreeterConf = {
name: 'xiao hua',
label: 'human'
}
//继承
interface humanConf extend GreeterConf{
player: Boolean
}
类属性
公有属性,私有属性,保护属性
class Person{
public name: String, //公有属性
private count: Number, //私有属性,不能被外部访问
protected family: String, //保护属性,只能被当前和继承类访问
}
class asia extend Person{
constructor() {
this.familyName = 'asia ' + this.family
}
}