-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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.9.2
Search Terms:
throw, return
Code
class Logger {
throwError() : never {
throw Error();
}
}
class Foo {
logger: Logger;
constructor() {
this.logger = new Logger();
}
// does not compile
fail(x : number) : number {
if (x < 0) {
return x;
} else {
const logger = new Logger();
logger.throwError();
}
}
// does compile
success1(x : number) : number {
if (x < 0) {
return x;
} else {
this.logger.throwError();
}
}
// does compile
success2(x: number, logger: Logger) : number {
if (x < 0) {
return x;
} else {
logger.throwError();
}
}
}
Expected behavior:
In all of these cases, the code should compile because logger.throwError()
will always throw an error, therefore returning from the current function.
Actual behavior:
However, for the fail
method, you get the compiler error
Function lacks ending return statement and return type does not include 'undefined'.(2366)
I'm not sure why the difference between having the logger as a field/parameter vs. a local variable causes some cases to compile but some not.
Related Issues:
mck1117 and namrog84benhamlin
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed