美文网首页
TS基础(十一)元组

TS基础(十一)元组

作者: Viewwei | 来源:发表于2020-12-25 20:00 被阅读0次

    数组合并想用类型的对象,而元祖(Tuple)合并了不同类型的对象
    元组可以属于理解成一个任意类型并且长度有限的数组

    简单的例子

    let tom: [string, number] = ['Tom', 25];
    
    
    let tom: [string, number];
    tom[0] = 'Tom';
    tom[1] = 25;
    
    tom[0].slice(1);
    tom[1].toFixed(2);
    

    元组越界

    当添加的元组越界的时候,越界的类型会被限制为元组类型中每个类型的联合类型

    let tom: [string, number];
    tom = ['Tom', 25];
    tom.push('male');
    tom.push(true);
    

    相关文章

      网友评论

          本文标题:TS基础(十一)元组

          本文链接:https://www.haomeiwen.com/subject/tafenktx.html