Typescript 为什么编译器隐式类型转换不适用于作为扩展接口实现的文本类型属性?

Typescript 为什么编译器隐式类型转换不适用于作为扩展接口实现的文本类型属性?,typescript,Typescript,为什么编译器隐式类型转换不适用于作为扩展接口实现的文本类型属性 例如: // typescript 3.4.5 interface INumContainer { num?: 1 | 2; } class myClass implements INumContainer { // does not compile // error TS2416: Property 'num' in type 'myClass' is not assignable to the same prop

为什么编译器隐式类型转换不适用于作为扩展接口实现的文本类型属性

例如:

// typescript 3.4.5

interface INumContainer {
  num?: 1 | 2;
}

class myClass implements INumContainer {
  // does not compile
  // error TS2416: Property 'num' in type 'myClass' is not assignable to the same property in base type 'INumContainer'.
  //   Type 'number' is not assignable to type '1 | 2 | undefined'.
  num = 1;

  anotherNum: 1 | 2 = 1; // compiles
}
至少从TS3.7开始,类成员不是按声明要扩展或实现的类型来定义的。
扩展
实现
声明确实会导致扩展/实现类的类型检查,但这种情况会在以后发生。长期以来,这一直是一个痛点

这里发生的事情是,像
1
2
这样的文本类型被扩展为包含像
number
这样的类型。由于
myClass.num
不是由
INumContainer[“num”]
上下文输入的,因此它被扩展到
number
,这被认为是不兼容的

关于财产的原始问题是。有很多方法可以修复它,但这是因为与一些实际代码库的互操作性较差

从那以后就没发生什么事了。目前关于这个主题的公开问题似乎是,所以如果你想看到这个问题得到解决,你可能想去那个问题,给它一个答案