-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unreachable code is not detected for methods that return never
#56049
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
This is working as intended. See #32695.
Your |
Interesting thank you - I've just added an explicit type for the |
Having looked at my actual code, it seems the same issue and resolution still applies. The intended pattern looks like this: errorManager
.error(
ConcurrencyMessageId._005_CONCURRENCY_REFRESH_REJECTION,
serviceError.status,
serviceErrorResponse['odata.error']?.code.toString() ?? '',
serviceErrorResponse['odata.error']?.message.value ?? '',
)
.throw({
args: [
['httpStatus', Number],
['errorCode', String],
['errorMessage', String],
],
});
// TS is not aware that core here is unreachable Where the return type of Based on your advice, unfortunately it seems the only way to get TS to correctly infer the control flow, is to break up the function chain and explicitly apply that inferred type to the error context: const errorContext: IErrorContext<[httpStatus: number, errorCode: string, errorMessage: string]> = errorManager
.error(
ConcurrencyMessageId._005_CONCURRENCY_REFRESH_REJECTION,
serviceError.status,
serviceErrorResponse['odata.error']?.code.toString() ?? '',
serviceErrorResponse['odata.error']?.message.value ?? '',
);
errorContext.throw({
args: [
['httpStatus', Number],
['errorCode', String],
['errorMessage', String],
],
});
// TS is aware that any core here is unreachable This is obviously quite verbose and a no go for DX given it makes the inference provided redundant by forcing the author to explicitly type it. So still curious is this is really the intention / expected behaviour here? If so, could it be improved? |
It is, and it's done for performance reasons. It's unlikely for change. |
@MartinJohns I'm curious what is the expected behaviour for explicit type when there are multiple chainable options (I have depth of 2 in my example below). Currently it doesn't have 7027 "unreachable code detected" I believe it meets all the criterias that you've mentioned above (correct me if I'm wrong) TypeScript Playground - https://tsplay.dev/Ndy4dm |
Having explicit types is just one requirement. See the list above.
Your |
@MartinJohns so |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
🕗 Version & Regression Information
never
⏯ Playground Link
https://tsplay.dev/wQqAZw
💻 Code
🙁 Actual behavior
If a function that returns
never
is a method, it does not result in error 7027 "unreachable code detected" or anyexpected type-narrowing for subsequent code when called.
🙂 Expected behavior
Any code after a call to a function that always returns
never
should result in error 7027 "unreachable code detected" and any expected type-narrowing regardless of whether that function is method or a standalone function.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: