-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Erroneous type narrowing in finally #18478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We should also handle function f(x: boolean) {
let a: boolean = false;
try {
a = true;
if (x) throw new Error();
a = false;
}
catch (e) {
// this is possible, should be allowed to test
a === true;
}
} |
Also facing this while setup a DB connection and trying to finalize it under the let connection;
try {
connection = await getConnection();
const { rows } = await connection.query(sql, values);
return camelizeKeys(rows);
} finally {
if (connection) {
// [ts] Property 'end' does not exist on type 'never'. [2339]
await connection.end();
}
} |
I'm facing the SAME issue, trying to finalize a db connection. What I could figure out is that it happends when a function returns anything at all, it presents the error, but if it's a void function, the issue is not present. also the code below:
|
I can't reproduce the earlier problems in this issue anymore (using Commenting-out either the Now, if you slightly tweak the example (to use function findById2(id: string) {
// not explicitly initializing this to `= undefined` prevents the problem
let connection: string | undefined = undefined;
try {
connection = id;
// commenting-out this `return` -OR- the `throw` below prevents the problem
return connection;
} catch (e) {
console.log(e);
// commenting-out this `throw` -OR- the `return` above prevents the problem
throw e;
} finally {
if (connection) {
// ts(2339) -
// Property 'length' does not exist on type 'never'.
console.log(connection.length);
}
}
} And this "work-around" reminds me of this CFA conversation... only there's no function expression involved. |
@SlurpTheo fixed in nightly; see #34880 |
TypeScript Version: Version 2.6.0-dev.20170914
Code
Expected behavior:
No compilation errors.
Actual behavior:
Compilation error:
bug.ts(8,13): error TS2365: Operator '===' cannot be applied to types '"No"' and '"Yes"'.
Note:
This bug is similar to "Erroneous control flow type narrowing in loops": #15835
The text was updated successfully, but these errors were encountered: