typeScript 数组 对象 解构 扩展

// 数据结构
let arra: number[] = [100, 200, 300, 400];
let arrb: Array<number> = [500, 600, 700, 800]

//  全结构
let [one, two, three, four] = arra;
//部分结构
let [o] = arrb;
let [, t] = arrb;
let [, , , f] = arrb;


console.log(one, two, three, four, '======123');
console.log(o, '第一位', t, '第二位', f, '第四位')

// 对象结构

//通用对象
let obja: any = {
    namea: 'zs',
    age: 18,
    sex: '女'
};

let { namea, age, sex } = obja;

console.log(namea, age, sex, '==========')

interface objb {
    nameb: string,
    ageb: number,
    sexb: string

}

let objb: objb = {
    nameb: 'ls',
    ageb: 19,
    sexb: 'nan'
}

let { nameb, sexb } = objb;

console.log(nameb, sexb, '=========')

//  结构对象重名

interface objc {
    namec: string,
    agec: number,
    sexc: string
}

let objc: objc = {
    namec: 'lis',
    agec: 19,
    sexc: '女'
}

let { namec: c1, agec: c2, sexc: c3 } = objc
console.log(c1, c2, c3, '==========123')

let c11: string;
let c22: number;
let c33: string;
({ c11, c22, c33 } = {
    c11: 'lis',
    c22: 19,
    c33: '女'
})
console.log(c11, c22, c33)

// 解构赋值
let [a, aa = 20] = [10];
console.log(a, aa)


// 解构赋值
let ja, jb
({ ja, jb } = { ja: 'aa', jb: undefined })
console.log(ja, jb, '============')

// 解构对象重赋值
let { one: _one = '哈哈', two: _two = 'xx' } = { one: '我是one' }
console.log(_one, _two, '====')

//结构用于函数参数
function x([first, second]: [number, number]): void {
    console.log(first);//1
    console.log(second);// 2
}
x([1, 2])

// 解构用于函数参树并设置默认值
function cc({ color, age = 33 }: { color: string, age?: number }): void {
    console.log('color的值:' + color, "age的值为:" + age)
}
cc({ color: 'aaa', age: 20 })
cc({ color: 'red' })

// 剩余模式
let [a1, b1, ...c] = [1, 2, 3, 4, 8]
console.log(a1, b1, c)

// 剩余模式用于解构
let oo = { texta: '我是a', textb: '我是b', textc: '我是c' }
let { texta, ...passthrough } = oo
console.log(texta, passthrough, '333333333333')

// 数组的展开操作
let onee: number[] = [1, 2]
let toww: number[] = [3, 4]
let threed: (number | string)[] = [0, ...onee, ...toww, 5]
console.log(onee, toww, threed)

// 对象展开
interface defaultobj {
    name: string,
    age: number,
    color: string
}
let defaultobj: defaultobj = {
    name: 'zs',
    age: 18,
    color: 'yellow'
}
interface targetboj1 {
    name: string,
    age: number,
    color: string,
    des: string
}
let targetobj: targetboj1 = {
    ...defaultobj,
    des: '描叙',
    name: 'xx'
}

// 函数
function addd(x: number, y: number): number {
    return x + y
}
let result = addd(2, 3)
console.log(result, addd(2, 3))

// 高阶函数 参数和返回值也是函数
let sume = (a: number, b: number, callback: (result: number) => void): void => {
    callback(a + b)
}

sume(100, 200, (result: number): void => {
    console.log('result=' + result)
})

// 命名空间

namespace wenDem {
    let a: string = '命名空间wenDemo中的变量a'
    // 接口
    interface PersonInterface {
        name: string,
        getInfo()
    }
    export class Person implements PersonInterface {
        name: string;
        constructor({ name }) {
            this.name = name
        }
        getInfo() {
            return "姓名:" + this.name
        }
    }
}

let pp = new wenDem.Person({ name: 'xxx' })
console.log(pp.getInfo(), '====123')




















你可能感兴趣的:(typescript)