-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 3.1.0-dev.20180824
Search Terms: Type Inference, Union Type, Assignment Operation
Code
// Tested by running `tsc` on the command-line
// strictNullChecks: on
interface BarA {
label: true;
yoha?(): string;
}
interface BarB {
label: false;
yoha?(): void;
}
class Foo {
bar!: BarA | BarB;
pia(): void {
if (this.bar.yoha) {
this.bar.yoha(); // passed compiling
}
if (this.bar.yoha) {
if (this.bar.label) {
let label = this.bar.yoha(); // passed
} else {
this.bar.yoha(); // passed
}
this.bar.yoha(); // passed
}
let bar = this.bar;
if (bar.yoha) {
bar.yoha(); // passed
}
if (bar.yoha) {
if (bar.label) {
let label = bar.yoha(); // error while compiling: Object is possibly 'undefined'.
} else {
bar.yoha(); // error: Cannot invoke an object which is possibly 'undefined'.
}
bar.yoha(); // error: Cannot invoke an object which is possibly 'undefined'.
}
}
}Expected behavior:
bar should behave the same as this.bar since they are indentical and passed by the simplest assignment operation.
Actual behavior:
Type inference for bar seems to break down after being initialized as the indentical of this.bar, shown as the demonstration code.
Related Issues: not yet
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created