Skip to content
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

Inconsistent behavior of the never return type between function and class method #61464

Closed
Septh opened this issue Mar 22, 2025 · 4 comments
Closed

Comments

@Septh
Copy link

Septh commented Mar 22, 2025

🔎 Search Terms

never return type

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&moduleResolution=99&esModuleInterop=false&strictBuiltinIteratorReturn=false&useUnknownInCatchVariables=false&alwaysStrict=false&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&suppressImplicitAnyIndexErrors=false&suppressExcessPropertyErrors=false&allowUnreachableCode=true&declaration=false&jsx=0#code/JYOwLgpgTgZghgYwgAgCoAtQHNkG9kAOUA9gQFzIDOYU2yAvgFAAmECANnFCjAK4gIwwYiGRYIYAMrEAthMwgsACgCUFDHQA+yEL3btGjAPRHkAQWR8BQkcjDo4YHRABu0ZNzC8oISoyuCwqLwwOyqFCCu7riMyHF26CQA7s4pAKJQJFBKAOTEpJQ5KoxMjAgi1AnYAIzIALxiEtJy9tiqjMAwyEoAhK2K1cXxlnCh7f1Y1QB0RKTxJsgAPAC0y1UDyMCUaAo4gKDkhgsWHHCU20nA9shwyC3oxMwJjs5uUB4S3r5lnGfIaQAel2isXiITCahewOGw3syVSf0yxGyeQKRRBcSYpXKvic-xo9XhAKB2WK2MqEwATATxFJZPI2sVOt0+rsKUN4nioFMweNWTMSAR5qYVmtKZtthpFMhtLp9MhADLkhmMpgAQrwnPYUHAAEbENzIJJIgDW53QEFEmuQxG1ACs2E4toRTpQII9TtcnVA4C13IA+DcAKLv+fiBWwwEBKCCAyBQChE6MqPDo5BkjW7ADM1KadIm7WGTN6EzT7OhyEjxO5o3BScL-LmcQWIvWWAzjsl+xKjCAA

💻 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

@Septh Septh changed the title Inconsistent handling of the never return type Inconsistent behavior of the never return type between function and class method Mar 22, 2025
@MartinJohns
Copy link
Contributor

This is working as intended. See #32695, or search for the many duplicates using never in:title label:Duplicate.

@Septh
Copy link
Author

Septh commented Mar 22, 2025

I'm not sure I'm following you there. This issue is about never as a return type not having the same effect depending on whether it is used on a plain function or on a class method (please see the code provided and/or the playground). Unless I am missing something, this not what #32695 is about.

@jcalz
Copy link
Contributor

jcalz commented Mar 22, 2025

It is indeed #36295.

A function call is analyzed as [a] never-returning call when […]

  • the call specifies a single identifier or a dotted sequence of identifiers for the function name, and
  • each identifier in the function name references an entity with an explicit type, and
  • the function name resolves to a function type with […] an explicit never return type annotation.

An entity is considered to have an explicit type when it is declared as a function, method, class or namespace, or as a variable, parameter or property with an explicit type annotation. (This particular rule exists so that control flow analysis of potential assertion calls doesn't circularly trigger further analysis.)

Your second example doesn’t work because xtr doesn’t have an explicit type. It will work if you annotate your variable like const xtr: Exiter = new Exiter().

@Septh
Copy link
Author

Septh commented Mar 22, 2025

Your second example doesn’t work because xtr doesn’t have an explicit type. It will work if you annotate your variable like const xtr: Exiter = new Exiter().

Ok, I did not get that. I assumed new would assign the correct type to the variable, I missed the part about the type needing to be explicit.

Thanks to both of you, I'm closing this.

@Septh Septh closed this as completed Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants