Closed
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
Metadata
Metadata
Assignees
Labels
No labels