简体   繁体   English

打字稿2.8.3:没有共同属性的联合类型

[英]Typescript 2.8.3: Union Types without common property

The following worked fine on Typescript 2.3.5 but it does not anymore on 2.8.3 . 以下内容在Typescript 2.3.5上运行良好,但在2.8.3不再可用。

class A {
       variableA: string;
   }
   class B {
       variableB: number;
   }
   class C {
       variableC: boolean;
   }
   type TYPES = A | B | C;

   function doStuff(types: TYPES) {
       switch (types) {
       case A: // error
           break;
       case B: // error
           break;
       case C: // error
           break;
       }
   }
   doStuff(A); // error

[ts] Argument of type 'typeof A' is not assignable to parameter of type 'TYPES'. [ts]类型'typeof'的参数不能分配给类型'TYPES'的参数。 Type 'typeof A' is not assignable to type 'C'. 类型“ typeof A”不可分配给类型“ C”。 Property 'variableC' is missing in type 'typeof A'. 类型“ typeof A”中缺少属性“ variableC”。

How do I change this structure so it works again? 如何更改此结构,使其再次起作用?

A type designates an instance of A/B/C class, while types value is A/B/C class itself. A类型表示A / B / C类的一个实例,而types值是A / B / C类本身。

It should be: 它应该是:

   type TYPES = typeof A | typeof B | typeof C;

   function doStuff(types: TYPES) {
       switch (types) {
       case A:
           break;
       case B:
           break;
       case C:
           break;
       }
   }
   doStuff(A);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM