TypeScript-基础类型&&接口

// 定义boolean
const isBoy: boolean = true

// 定义number
const one: number = 23

// 定义string
const file:string = 'qiangfeng'

//定义数组
const list : number[] = [1,3,4]
const list2: Array = ['1','2','3']

// 定义元组
const x : [string,number]= ['qinagfeng',26]

//定义枚举
enum Sex {male='男', famel='女'}
const c: Sex = Sex.famel
console.log(c)

// 定义any
let notsure: any = 4
notsure ='1111'
const array: any[] = [1,'2']

// 定义void
function warnUser(): void {
    console.log("This is my warning message");
}

// null与undefined类型
// 默认情况下null和undefined是所有类型的子类型。将strictNullChecks设置为false时,可任意赋值给所有类型
let abc:string
abc=null   

// 类型断言
const some:any='dsddj dsd'
const strLength:number = (some as string).length // as语法
const slh :number = ( some).length //箭头语法

//定义接口 
interface Student{
    name: string
    grade: number
}
const getStudentInfo = (stu: Student):string => `name is ${stu.name},grade is ${stu.grade}`
const sInfo = getStudentInfo({name:'bob',grade:2})
console.log(sInfo)

// 接口可选属性 
interface Teacher{
    name: string
    sex?:string
}
const getTeacherInfo = (teacher: Teacher):string => `name is ${teacher.name},sex is ${teacher.sex}`
const tInfo = getTeacherInfo({name:'bob'})
console.log(tInfo)

// 接口只读属性
interface Point {
    readonly x: number;
    readonly y: number;
}
let p1: Point = { x: 10, y: 20 };
// p1.x = 5; // error!

//绕过接口额外属性检查

// 1、使用断言
interface SquareConfig {
    color?: string;
    width?: number;
}
const createSquare = (config: SquareConfig): { color: string; area: number } => ({
    color: config.color||'',
    area:2
})
let mySquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig);


// 2、字符串索引签名
interface SquareConfig2 {
    color?: string;
    width?: number;
    [opacity: string]: any;
}
const createSquare2 = (config: SquareConfig2): { color: string; area: number } => ({
    color: config.color||'',
    area:2
})
let mySquare2 = createSquare2({ width: 100, opacity: 0.5 });


// 3、将这个对象赋值给一个另一个变量
const temp = { width: 100, opacity: 0.5 }
let mySquare3 = createSquare(temp);


//函数类型接口
interface Callback{
    (err: string, res: any[]): void
}
const ajaxResponse = (url: string,callback: Callback )=> {
    //...
    callback(null,[1,2,2])  //关闭strictNullChecks
}
ajaxResponse('/abc',(err,res)=>{
    if (!err){
        console.log(res)
    }
})

// 类类型
interface ClockInterface {
    currentTime: Date
    setTime(d: Date):void   //接口中定义方法
}
let clock:ClockInterface = {
    currentTime: new Date(),
    setTime: function(newTime:Date):void {
        this.currentTime = newTime
    }
}
clock.setTime(new Date('2010-11-11'))
console.log(clock.currentTime)


// 接口继承
interface Animal{
    sex: string
}
interface CatType{
    color: string
}
interface Dog extends Animal,CatType{ //可多继承
    sleep():void
}
let dog =  {} //断言
dog.sex='male'

//混合类型
 interface School {
     (location: string):string //构造函数
    students: number
    getStudy(): number
 }

 let getSchool = function():School {
    const school =  function(location: string){}
    school.students =20
    school.getStudy = ():number => 29
    return school
 }

 // 接口继承类
 // 接口同样会继承到类的private和protected成员。 
 // 这意味着当你创建了一个接口继承了一个拥有私有或受保护的成员的类时,这个接口类型只能被这个类或其子类所实现(implement)。
class Control {
    private state: any;
}

interface SelectableControl extends Control {
    select(): void;
}


class Button extends Control implements SelectableControl {
   select(){}
}


















你可能感兴趣的:(TypeScript-基础类型&&接口)