Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ts联合类型(_)与交叉类型(&)

联合类型(|)

  • 一般用于基本类型定义
    let x: number | string = "aaa";
    let y: number | string = 123;
    
    • 对象既可以是字符串类型,也可以是数字类型
    • 取的是两个的并集,两个中任一个都可以

交叉类型(&)

  • 一般用于interface

    interface Man {
        run(): void;
    }
    
    interface Worker {
        work(): void;
    }
    
    interface maleWorker: Man & Worker = {
        run() { },
        work() { }
    }
    
    • 取的是两个的合并,两个都必须要有