如果我们在打字稿上有这个结构:
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?