TypeScript Version: 3.3.0-dev.20181130
type inference
type inference inheritance
type inference inheritance optional
type inference inheritance optional property
type inference inheritance optional union type
type inference inheritance optional property union type
Code
export class ModelBase {
modelBaseProp: boolean;
constructor() {
}
}
export class Model1 extends ModelBase {
model1Prop?: number;
constructor() {
super();
}
}
export class Model2 extends ModelBase {
model2Prop?: string;
constructor() {
super();
}
}
export class Test {
test: Model1 | Model2;
constructor() {
this.test = new Model1();
this.test.model1Prop = 1;
this.test = new Model2();
this.test.model2Prop = 'test';
}
}
Expected behavior:
No compilation errors.
Actual behavior:
error TS2339: Property 'model1Prop' does not exist on type 'Model1 | Model2'. Property 'model1Prop' does not exist on type 'Model2'
and
error TS2339: Property 'model2Prop' does not exist on type 'Model1 | Model2'. Property 'model2Prop' does not exist on type 'Model1'.
Playground Link: link
Additional info Making the properties in the derived classes non-optional works as expected. Note that the following is working properly: Playground link
export class Model1 {
model1Prop?: number;
constructor() {
}
}
export class Model2 {
model2Prop?: string;
constructor() {
}
}
export class Test {
test: Model1 | Model2;
constructor() {
this.test = new Model1();
this.test.model1Prop = 1;
this.test = new Model2();
this.test.model2Prop = 'test';
}
}
TypeScript Version: 3.3.0-dev.20181130
type inference
type inference inheritance
type inference inheritance optional
type inference inheritance optional property
type inference inheritance optional union type
type inference inheritance optional property union type
Code
Expected behavior:
No compilation errors.
Actual behavior:
error TS2339: Property 'model1Prop' does not exist on type 'Model1 | Model2'. Property 'model1Prop' does not exist on type 'Model2'and
error TS2339: Property 'model2Prop' does not exist on type 'Model1 | Model2'. Property 'model2Prop' does not exist on type 'Model1'.Playground Link: link
Additional info Making the properties in the derived classes non-optional works as expected. Note that the following is working properly: Playground link