[Typescript 5.3] Switch true statement for type narrowing

class Fish {
  swim(): void {}
}

class Bird {
  fly(): void {}
}

switch(true) {
    case val instanceof Bird:
        val.fly()
        break
    case val instanceof Fish:
        val.swim()
        break
}

switch(true): means that it will validate each case, until it finds first when validated as true

posted @ 2024-01-22 14:44  Zhentiw  阅读(1)  评论(0编辑  收藏  举报