const {ccclass, property} = cc._decorator;
const LEVEL = cc.Enum({EASY:1,HARD:2});
@ccclass
export default class Helloworld extends cc.Component {
// 整型
@property(cc.Float)// @property()
intVar: number = 0;
@property(cc.Float)// @property()
intVarOther = 0;
// 浮点型
@property(cc.Float)// @property()
floatVar: number = 0;
@property(cc.Float)// @property()
floatVarOther = 0;
// 布尔型
@property(cc.Boolean)// @property()
boolVar: boolean = false;
@property(cc.Boolean)// @property()
boolVarOthre = false;
// 点
@property(cc.Vec2)// @property()
vec2Var: cc.Vec2 = cc.v2();
@property(cc.Vec2)
vec2VarOther = cc.v2();
//字符串
@property()
text: string = 'hello';
@property()
textOther = 'hello';
// 节点
@property(cc.Node)
nodeVar: cc.Node = null;
// @property(cc.Node)
// nodeVarOther = new cc.Node;
// 节点数组
@property([cc.Node])
nodeArrVar: Array<cc.Node> = [];
@property([cc.Node])
nodeArrOther: cc.Node[] = [];
@property([cc.Node])
nodeArrAny: any[] = [];
// @property()
// nodeArrOtherO : any = null ;
// Label
@property(cc.Label)
label: cc.Label = null;
@property(cc.Label)
labelOther = new cc.Label;
// 预制体
@property(cc.Prefab)
prefabVar: cc.Prefab = null;
@property(cc.Prefab)
prefabVarOther = new cc.Prefab;
//任意类型
anyObj: any;
// @property
// _width = 100;
// @property
// get width () {
// return this._width;
// }
// // 自定义节点
// @property(Player)
// palyerVar: Player = null;
// 重点来了,自定义枚举
//枚举类型
@property({
type:LEVEL
})
enumVa = LEVEL.EASY;
//完整属性定义
@property({
visible:false
})
textVisible: string = 'helloVisible';
onLoad(){
console.log("onLoad");
}
start () {
// init logic
this.label.string = this.text;
let person = "tom";
console.log(this.sayHello(person));
//任意类型
let personAny :any= "personAny"
console.log(this.sayHello(personAny));
personAny = 111;
console.log(this.sayHello(personAny));
console.log(this.sayHello(null));
//类型推论
let myFavoriteNumber;
myFavoriteNumber = 'seven';
console.log(myFavoriteNumber);
myFavoriteNumber = 7;
console.log(myFavoriteNumber);
//联合类型
let myFavoriteNumber1: string | number;
myFavoriteNumber = 'seven';
myFavoriteNumber = 7;
//联合类型只能访问共有方法,或者当属赋值后类型的方法
if ((<string>myFavoriteNumber).length) {//断言类型
return (<string>myFavoriteNumber).length;
} else {
return myFavoriteNumber.toString().length;
}
// let tom:Helloworld = {
// }
// console.log(tom);
console.log(this.sum(1,1));
}
sayHello(person: string) {
return 'Hello, ' + person;
}
//函数表达式 后边可选参数要放后边
sum(x:number,y?:number):number{
return x+y;
}
//函数表达式 默认值参数,视作可选参数 位置随意
sum1(x:number,y:number = 1):number{
return x+y;
}
//剩余参数 相当于sum2(x:number,...items:any[])
sum2(x:number,...items):number{
return x+1+2+3;
}
//重载 优先匹配
sum3(x:number):number;
sum3(x:string):string;
sum3(x:number|string):number|string{
if (typeof x === 'number') {
return Number(x.toString().split('').reverse().join(''));
} else if (typeof x === 'string') {
return x.split('').reverse().join('');
}
}
//函数表达式
sum4(){
//未指定返回类型
let mySum = function (x: number, y: number): number {
return x + y;
};
//指定返回类型
let sum:(x:number,y:number) => number = function(x:number,y:number):number{
return x+y;
}
}
}
declare namespace jQuery {
interface AjaxSettings {
method?: 'GET' | 'POST'
data?: any;
}
function ajax(url: string, settings?: AjaxSettings): void;
}
参考资料
https://zhongsp.gitbooks.io/typescript-handbook/content/
https://docs.cocos.com/creator/manual/zh/scripting/typescript.html