Typescript引用类型--数组

ts中数据分为值类型和引用类型。
一、什么是引用类型?
引用类型是一种复合类型,引用类型中封装了很多属性,可以通过变量名和属性名来获取属性值或者调用属性的方法。

    let Jude = {
        name: 'Jude',
        age: 28,
        saySomething: function () {
            console.log('行到水穷处,坐看云起时!');
        }
    }
    console.log(Jude.name) // Jude

二、元祖--一种特殊的数组
数组中允许含有多种类型的元素

    let arr : [number,string]
    arr = [123,'123']
    // console.log(arr) // [123,'123]
当我们写成这个样子,是会报错的
    let arr : [number,sting]
    arr = ['123',123]
error1: Type 'string' is not assignable to type 'number'. 
error2: Type 'number' is not assignable to type 'string'.


![](https://img2018.cnblogs.com/blog/1912711/202001/1912711-20200119172755771-1404695424.png)

猜你喜欢

转载自www.cnblogs.com/judeyq/p/12214394.html