1.typescript代码练习

let message:string ='hello nice day';
console.log(message);  
console.log("ok,good luck11111111111");
let a = 1+2;
let b= a+3;
let c={
    apple:a,
    banana:b
};
let d = c.apple * 4;
console.log(d);
let aa = true
let bb:boolean = false
let ee: true = true  //指定ee类型是true,就只能赋值true
let ff:false= false //指定ff类型是false,就只能赋值false
let a1 = 1234;
var b1 = Infinity *0.10
const c1 =5678
let e1:number = 100
let a_str = "hello"
let a_str2:string = "hello2"
console.log(a_str);

//let a2:bigint = 100
let a_obj:object = {
    b:"x"
} //无法访问b
let a_obj2 = { //对象
    b:"x"
} //可以访问b
console.log(a_obj2.b)
let a_obj3:(b:number)=>
{
    b:11
}
//console.log(a_obj3.b) 无法访问
let c_a:{
    firstName:string
    lastName:string
}={
    firstName:"t",
    lastName:"zhan"
}
c_a.firstName //可以访问
class Person{
    constructor(
        public firstName:string,
        public lastName:string
    ){}
}
c_a = new Person("mt1","zhou")
let i:number=3; //声明变量,必须赋值
let j = i*3;
//索引
let airplaneSeatingAssignments:
{
    [seatNunber:string]:string
} = {
    "2D":"tt",
    "2F":"zz5"
}
let loc2 = airplaneSeatingAssignments["2D"]
console.log("索引:"+loc2)
airplaneSeatingAssignments["2D"]="t3x"
loc2 = airplaneSeatingAssignments["2D"]
console.log("索引:"+loc2)

let user:{
    readonly firstName:string,
    lastName:string
} ={
    firstName:"lubby",
    lastName:"nice"
}
//user.firstName = "11"
user.lastName = "ss"
type Color = "red"
//type Color ="blue"
let x = Math.random()<.5
if(x)
{
    type Color ="blue" //type类型别名
    let b:Color ='blue'
}
else
{
    let c:Color="red"
}
//DRY是什么意思:Don't Repeat Yourself
//并集,| ,交集:&
type Cat = {name:string,purrs:boolean}
type Dog = {name:string,barks:boolean,wags:boolean}
type CarOrDogOrBoth = Cat|Dog
type CatAndDog = Cat & Dog
let a11:CarOrDogOrBoth = {  //cat
    name:"bonk",
    purrs:true
}
a11 = {  //dog
    name:"domi",
    barks:true,
    wags:true
}
//二者兼具
a11 ={
    name:"donk",
    barks:true,
    purrs:true,
    wags:true
}
//交集
let b11:CatAndDog ={
    name:"domin",
    barks:true,
    purrs:true,
    wags:true
}
function trueOrNull(isTrue:boolean)
{
    if(isTrue)
    {
        return "true"
    }
    return null;
}
type Returns = string | null
function aaa(a:string,b:number)//函数创建,方法
{
    return a||b
}
//数组
let a_group = [1,2,3]
var b_g= ['a','b']
let c_g:string[] =["a"]
let d_group =[1,"a"] //(string|number)[]

let f_group =["red"]
f_group.push("blue")
f_group.push("ccd")
//f_group.push(true)
let g=[] //any[]
g.push(1)
g.push('red') //(string|number)[]
let hh:number[] = []//number[]
hh.push(1)
//hh.push("read")//指定Number就只能赋值Number
let d_47 = [1,"a"]
d_47.map(ff=>{
    if(typeof(ff)=="number")
    {
        return ff*3
    }
    return ff.toUpperCase()
})
function buildArray2()
{
    let a=[]
    a.push(1)
    a.push("x")
    return a
}
let myArr = buildArray2()  
//myArr.push(true)






posted @   txwtech  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示