Typescript接口上的类型推断

时间:2020-09-17 14:37:53

标签: typescript types interface structure inference

如果我们在打字稿上有这个结构:

type A = {
  v: number | string[]
}

interface B {
  a: A;
}

class C implements B {
  a = {
    v: 3 // inference only with type number, string[] is lost
  } 
}

c = new C();
c.a.v = ['hello']; // not working, needs a: A on class C


// Working class must be:

class C implements B {
  a: A = {
    v: 3
  } 
}

有人知道是否有一种变通方法,以避免在每个类的“ a”上设置de type?

0 个答案:

没有答案