typeScript学习-类型断言、类型转换

发布时间 2023-08-22 16:26:24作者: 空白格k

typeScript学习

类型断言、类型转换

1、类型断言:

语法格式:A 数据类型的变量 as B 数据类型。

let b: B

let c: C = b as C

理解:是绕过TS 编译检查,类型断言就是对编译器说:我是这个类型了,无需检查。

2、类型断言使用场景

export class Vechile {
    static count: number = 3
    public brand: string // 品牌
    public vechileNo: string // 车牌号
    public days: number // 租赁天数
    public total: number = 0 // 支付的租赁总费用
    constructor(brand_: string, vechileNo_: string, days_: number) {
        this.brand = brand_
        this.vechileNo = vechileNo_
        this.days = days_
    }
    public calculateRent() {
        console.log(this.brand + " 车牌号:" + this.vechileNo + "开始被租")
        return 0
    }
}

class Car extends Vechile {
    public type: string // 车的型号
    public brand: string = "Car品牌" // 品牌
    constructor(brand_: string, vechileNo_: string, days_: number, type_: string) {
        super(brand_, vechileNo_, days_)
        this.type = type_
    }
    // 根据车的型号来获取租用一天该型号车的租金
    public getPriceBytype() {
        let rentMoneyByDay: number = 0 // 每天的租金
        if (this.type === "普拉多巡洋舰") {
            rentMoneyByDay = 800
        } else if (this.type === "凯美瑞旗舰版") {
            rentMoneyByDay = 400
        } else if (this.type === "威驰智行版") {
            rentMoneyByDay = 200
        }
        return rentMoneyByDay
    }

    public calculateRent() {
        super.calculateRent()
        console.log("小轿车租赁...")
        return this.days * this.getPriceBytype()
    }
}


class Bus extends Vechile {
    public seatNum: number // 座位数
    constructor(brand_: string, vechileNo_: string, days_: number, seatNum_: number) {
        super(brand_, vechileNo_, days_)
        this.seatNum = seatNum_
    }

    public getPriceBySeatNum() {
        // 计算租赁价格
        let rentMoneyByDay: number = 0 // 每天的租金
        if (this.seatNum <= 16) {
            rentMoneyByDay = 800
        } else {
            rentMoneyByDay = 1600
        }
        return rentMoneyByDay
    }

    public calculateRent() {
        super.calculateRent()
        return this.days + this.getPriceBySeatNum()
    }
}

class Truck extends Vechile {
    ton!: number // 载量
    constructor(brand_: string, vechileNo_: string, days_: number, ton_: number) {
        super(brand_, vechileNo_, days_)
        this.ton = ton_
    }
    CalRentPrice() {
        // 计算租赁价格
        let rentMoneyByDay: number = 0 // 每天的租金
        if (this.ton <= 500) {
            // 500吨
            rentMoneyByDay = 750
        } else if (this.ton > 500) {
            rentMoneyByDay = 1350
        }
        return rentMoneyByDay
    }
    public calRent() {
        return this.CalRentPrice() * this.days
    }
}

class Customer {
    rent(vechile: Vechile) {
        // if (vechile instanceof Car) {
        let car = vechile as Car
        console.log("car.type:", car.type)
        return car.calculateRent()
        // }
    }
}

let cust: Customer = new Customer()
let vechile: Vechile = new Car("AA", "abc", 30, "普拉多巡洋舰")
// 类型断言
// let car: Car = vechile as Car
// 类型转换
let car: Car = <Car>vechile
console.log(car.calculateRent()) // 24000
vechile = new Truck("AA", "abc", 1, 300)

let truck: Truck = vechile as Truck
console.log(truck.calRent()) // 750


export { }

 

let symid = Symbol("userid")
let user = { [symid]: 101, username: "wangwu", age: 25 }
let name1 = "username"
let userid = user[symid]
// let username = user[name1] // 错误
let username = user[name1 as any]
test({ username: 'wangwu', age: 25 })
function test(data: unknown) { }
console.log("username:", username)
export { }

3、类型转换——编译器强制一个类型转换成另一个类型。