-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 2.1.0-dev.20160807
Code
class Nested {
data: string;
}
class Foo {
bar: Nested | null = null;
baz: number = 1;
get data(): null | string {
if (this.bar === null || this.baz !== 1) {
return null;
} else {
return this.bar.data;
// ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)
}
}
}
Expected behavior:
No error.
Actual behavior:
An error.
Details:
This failure must be new. I think I installed typescript@next
last week and had no error. I installed it today and it throws an error now.
If a you remove the second if-branch (this.baz !== 1
) it works.
This also throws:
get data(): null | string {
if (this.bar === null) {
return null;
} else if (this.baz !== 1) {
return null;
} else {
return this.bar.data;
// ^~~~~ Object is possibly 'null'. (BUT IT CAN'T BE NULL.)
}
}
This works:
get data(): null | string {
if (this.baz !== 1) {
return null;
} else if (this.bar === null) {
return null;
} else {
return this.bar.data;
}
}
(!) On the other hand: This works, but could be wrong so it should throw an error!
get data(): null | string {
if (this.baz !== 1 || this.bar === null) {
return null;
} else {
return this.bar.data;
}
}
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created