TypeScript Version: 3.8.0-beta onwards
Search Terms: return, iife, error, flow anaylsis, try catch
Code
const x = (() => {
try { return 'x'; } catch (e) {
return null;
}
})();
// any code here is unreachable according to TS
Expected behavior:
Typescript marks code as reachable
Actual behavior:
Typescript wrongfully assumes that any code after the block is unreachable
Playground Link:
https://www.typescriptlang.org/play/?ts=3.8-Beta&ssl=11&ssc=14&pln=17&pc=4#code/DYUwLgBAngXBBGB7RoCGA7A3AWAFB4DMBXdAYzAEtF0IBnRAW3AAsL0BzAFWdTAGFERYABMAYqgrAAFAEo4tMACc27CAG88ECBQIQpUGes1aIYZosQB3COhDWAooouKpAchBPEi1zJy4tAL7GiuBEijSuCsocrn5B+LjEZJTUEATIsvJKKkb+EKTUChAAHhAAvHqy5QB8uSamilB19RAhYGE09ExmKty8AkJiEtK+xoH5vKTMeiCGGnn1bR02QsB+9fGBMrJ+xjp6pWVHK8DAhkvhEK4EqKfwqKQA1rF4waGXxXFAA
Related Issues:
None found
Some more info:
I initially found this issue in version 3.9.0-dev.2000215 but found it to also be present in 3.8.0-beta. It it not present in the current stable version (3.7.5).
@RyanCavanaugh We should consider pulling this into 3.8.
This bug is preventing our team from upgrading to TS 3.8.
Hope the fix could be backported to 3.8! 馃槂
If not my mistake, this bug does not seem to be fixed in 3.8.3 :(
@ahejlsberg I'm glad if you can confirm.
thanks.
Playground Link:
https://www.typescriptlang.org/play/#code/DYUwLgBAngXBBGB7RoCGA7A3AWAFB4DMBXdAYzAEtF0IBnRAW3AAsL0BzAFWdTAGFERYABMAYqgrAAFAEo4tMACc27CAG88ECBQIQpUGes1aIYZosQB3COhDWAooouKpAchBPEi1zJy4tAL7GiuBEijSuCsocrn5B+LjEZJTUEATIsvJKKkb+EKTUChAAHhAAvHqy5QB8uSamilB19RAhYGE09ExmKty8AkJiEtK+xoH5vKTMeiCGGnn1bR02QsB+9fGBMrJ+xjp6pWVHK8DAhkvhEK4EqKfwqKQA1rF4waGXxXFAA
It looks like this has been fixed in the nightly at least (the playground doesn't have a 3.9.0-beta version option) - 3.9.0-dev.20200328
https://www.typescriptlang.org/play/?ts=3.9.0-dev.20200328&ssl=1&ssc=1&pln=12&pc=1#code/GYVwdgxgLglg9mABMMAVAFgQygWRgc3SgwCc4B3AIRCgBE4BTAZzCgAoBKRAb0QF8AUBARMoiAHIB5WgFEA+gEEAykpkAlVHKWo1ASQDCmmQEUAqgoAycyQAV1C1JLWIAvIjadXAPh4DE-xCgSAE9fAPDkNCxcAiJSCmo6RhZ2DgBuPwC+RAhsCHR3BhISLm5MiMQSBigQEiQikgA6OAAHIuw4EgzwwXCodDJyRDAGIZlizrYAInAGAA826AYAE0QmEAgIZiZQABtETCYmItgEKfSBPg5ODKEROF2GRt24fDYAcnQYd4ugA
The fix didn't make it into 3.8.3. It is fixed in 3.9 beta.
Most helpful comment
The fix didn't make it into 3.8.3. It is fixed in 3.9 beta.