There are several options (If fooVar is really supposed to be either one of Foo, Foo2 or Foo3).
The simplest solution is a type assertion :
class Goo {
fooVar: Foo | Foo2 | Foo3 = {
a: ""
};
constructor() {
(this.fooVar as Foo3).c = 0
}
}
If c is already in fooVar and you only want to assign it if it already exist (ie fooVar is already Foo3) you can use an in type guard:
class Goo {
fooVar: Foo | Foo2 | Foo3 = {
a: "", c: -1
};
constructor() {
if('c' in this.fooVar) this.fooVar.c = 0
}
}
If you don't mind changing the object reference you can use spread :
class Goo {
fooVar: Foo | Foo2 | Foo3 = {
a: "",
};
constructor() {
this.fooVar = {...this.fooVar, c: 0 }
}
}
fooVarshould use intersection rather than union types (Foo & Foo2 & Foo3), but it also depends on what you're doing specifically