Repository navigation
async type narrowing #62624
Copy link
Copy link
Closed as duplicate
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
🔎 Search Terms
"async", "Promise", "Promise<never>", "never", "type narrowing"
🕗 Version & Regression Information
- This is the behavior in every version I tried (incl. 5.9.3, nightly), and I reviewed the FAQ for entries about
async,Promise<never>,narrowing
⏯ Playground Link
💻 Code
function getValue(): string | null {
return "theValue"
}
async function doWork(): Promise<void> { }
/**
* Type-narrowing is applied. `x` satisfies the constraint `string`.
*/
function doSomethingSync(): string {
function fail(): never {
throw new Error()
}
const x: string | null = getValue()
if (x === null) fail()
return x satisfies string
}
/**
* Type-narrowing is applied. `x` satisfies the constraint `string`.
*/
async function doSomethingAsyncWithInlineFail(): Promise<string> {
const x: string | null = getValue()
if (x === null) {
await doWork()
throw new Error()
}
return x satisfies string
}
/**
* Type-narrowing is not applied. `x` does not satisfy the constraint `string` as it may be of type `null`.
*/
async function doSomethingAsyncWithAsyncFunctionFail(): Promise<string> {
async function fail(): Promise<never> {
await doWork()
throw new Error()
}
const x: string | null = getValue()
if (x === null) await fail()
return x satisfies string
}🙁 Actual behavior
doSomethingAsyncWithInlineFail and doSomethingAsyncWithAsyncFunctionFail behave differently - the former applies type-narrowing, the latter does not.
🙂 Expected behavior
doSomethingAsyncWithInlineFail and doSomethingAsyncWithAsyncFunctionFail should behave identically.
My personal preference would be to apply the behaviour of doSomethingAsyncWithInlineFail to doSomethingAsyncWithAsyncFunctionFail.
However, I understand this might not be technically correct as the event loop might execute code which mutates the type-narrowed value.
Additional information about the issue
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Activity
RyanCavanaugh commentedon Oct 17, 2025
Duplicate #34955
Lordfirespeed commentedon Oct 18, 2025
Thankyou for this - I'm sorry I didn't find it, I thought I'd done my due diligence 😭