2.typescript代码练习

 

//元组--需要显式的注解类型,数组
let a:[number] = [1]
let b:[string,string,number] = ["a","b",1]
//b = ["a","a","a"]错误
let trainFares:[number,number?][]=[  //中括号的问号表示可选
    [3.8],
    [8.3,6],
    [10.5]
]
//等价
let moreTrainFares:([number]|[number,number])[]=[[3.8],
[8.3,6],
[10.5]]
let friends:[string,...string[]] =['a','b'];
let aa:[number,...number[]] = [1,2,3]
console.log("元组的值:"+aa[1])
//元组类型不同的列表
let list2:[number,boolean,...string[]] = [2,true,'c','d','e']
console.log(list2[0])
console.log(list2[1])
console.log(list2[2]);
console.log(list2[3]);
console.log(list2[4]);
type A = readonly string[]
type B = ReadonlyArray<string>
type C = Readonly<string[]>
type D =readonly[number,string]
type E =Readonly<[number,string]>
function fun_a(x:number)//typescript函数,方法
{
    if(x<10)
    {
        return x
    }
    return null;
}
function func_b()
{
    return undefined
}
function func_c()
{
    let a= 2+2;
    let b = a*a;
}
function func_D()
{
    throw TypeError('al err') //typescript抛出异常
}
function func_e()
{
    // while(true)
    // {

    // }
}
//枚举
enum Language2
{
    achinease,
    english,
    spanish,
    russian
}
enum Language2a
{
    achinease=0,
    english=1,
    spanish=2,
    russian=3
}
let myFirstlan = Language2.achinease
let mySecondlan= Language2["english"]
const enum Flippable{
    Burger = "Gurger",
    Chair ="Chair",
    Cup ="cup",
    Skateboard ="Skateborad",
    Table ="Table"
}
function flip(f:Flippable)
{
    return 'flipped it la'
}
flip(Flippable.Cup)
flip(Flippable.Chair)
//flip(12)
//flip("hat")



posted @ 2024-03-26 13:59  txwtech  阅读(53)  评论(0编辑  收藏  举报