一 数据基本类型
let boo:boolean = true;
boo = false
let num:number = 123;
let str:string = "你好啊";
let arr: Array<number> = [1,2,3];
let arr2: string[] = ['1','2']
let _undefined:undefined = undefined;
let _null:null = null;
let any_var:any = 123;
any_var = true;
二ts中的或与非
let some_var:number | string | boolean | undefined = 123;
some_var = true;
some_var = "aaa";
some_var = "undefined"
let person4:IB & IC & ID = {
ib: '11',
ic: '22',
id: '333'
}
三 对象接口
function func(arg:number):void {
}
func(111)
function func2(arg:string):boolean {
return false
}
interface IPerson {
name: string;
age: number;
sex: string;
}
let obj:IPerson = {
name: "张三",
age: 20,
sex: "张三"
}
let obj2:IPerson = {}
interface IPerson2 {
name?: string;
age?: number;
sex: string;
}
let obj3:IPerson2 = {
sex:'男'
}
interface IA extends IPerson2 {
hobby: Array<string>;
}
let person2: IA = {
sex: '男',
hobby: ['hello','world']
}
interface IB {
ib: string
}
interface IC {
ic: string
}
interface ID extends IB,IC {
id: string
}
let person3:ID = {
ib: '11',
ic: '22',
id: '333'
}
interface IPerson6 {
uname: string;
uage: number;
usex:string;
getName:() =>string;
getAge ():number;
}
class Person implements IPerson6 {
uname: string = '张三';
uage: number = 20;
usex:string;
getName (): string{
return this.uname;
};
getAge ():number {
return this.uage;
};
sayHello ():void {
}
}
interface IPerson5 {
[propName: string] :any;
}
let person5:IPerson5 = {}
function getLength (str: number | string):number {
if ((str as string).length) {
return (<string>str).length
}else {
return str.toString().length
}
}
function fn3 (arg?: string):number {
return arg!.length;
}
enum Color {
red,
green,
yellow
}
console.log(Color.red)
console.log(Color.green)
console.log(Color[1])
enum Color2 {
red = 1,
green,
yellow
}
function fun4<T>(arg:T):void {
}
fun4<number>(123)
fun4<string>('abc')