-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
TypeScript Version: 3.8.0-dev.20200211
Search Terms: type narrowing typescript function
Code
class ThrowingClass {
public thrower(): never {
throw new Error("oh no!");
}
}
function needs_narrowing(): string | null {
if (Math.random() > 0.5) {
return "Narrow me";
} else {
return null;
}
}
function string_needer(value: string): void {
console.log(value);
}
const throwing_class = new ThrowingClass();
const to_narrow = needs_narrowing();
if (to_narrow === null) {
throwing_class.thrower();
}
string_needer(to_narrow);
Expected behavior: TypeScript compiler sees that the method of the instantiated class never returns and therefore deduces that this is safe, like it does in standalone functions and static methods (see "Working example").
Actual behavior: TypeScript compiler does not consider whether the method of the instantiated class never returns and therefore deduces that this is unsafe.
jsejcksn and anjianshi
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed