How do you emulate ADTs and pattern matching in TypeScript?

前端 未结 6 1396
清酒与你
清酒与你 2020-12-20 13:53

Unfortunately, as of 0.9.5, TypeScript doesn\'t (yet) have algebraic data types (union types) and pattern matching (to destructure them). What\'s more, it doesn\'t even supp

6条回答
  •  臣服心动
    2020-12-20 14:18

    To answer

    it doesn't even support instanceof on interfaces.

    Reason is type erasure. Interfaces are a compile type construct only and don't have any runtime implications. However you can use instanceof on classes e.g. :

    class Foo{}
    var x = new Foo();
    console.log(x instanceof Foo); // true
    

提交回复
热议问题