-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Closed
Copy link
Description
🔎 Search Terms
never return type
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
interface Thing { prop: any }
declare function getSomething(): Thing | null
// A function that never returns
function fail(): never {
throw new Error('oops')
}
const thing1 = getSomething()
if (!thing1)
fail()
thing1.prop // <-- ✅ thing1 is Thing
// A class with a method that never returns
class Exiter {
fail(): never {
throw new Error('oops')
}
}
const xtr = new Exiter()
const thing2 = getSomething()
if (!thing2)
xtr.fail()
thing2.prop // <-- ❌ thing2 is Thing | null (ts18047)
// But the above works when the Exiter instance is passed as a parameter 🤔
function fn(exiter: Exiter) {
const thing3 = getSomething()
if (!thing3)
exiter.fail()
thing3.prop // <-- ✅ thing3 is Thing
}🙁 Actual behavior
'thing2' is possibly 'null'. (ts18047) on case 2.
🙂 Expected behavior
Should compile.
Additional information about the issue
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels