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

Specifying return type 'never' on function expression doesn't work, but it works on function declaration. #57732

Closed
1 task done
yyijoo opened this issue Mar 12, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@yyijoo
Copy link

yyijoo commented Mar 12, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

In the code below, mainBar and mainQux are exhibiting errors as if the return type of bar() and qux() were 'undefined'.
Is it by intention or is it kind of a bug?
If it's so by intention, What's inferred differently?

function foo(): never {
  throw new Error();
}

const bar = (): never => {
  throw new Error();
}

const qux = function():never {
    throw new Error();
}

function mainFoo(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  foo();
}

function mainBar(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  bar();
}

function mainQux(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  qux();
}

(playground)

enter image description here

@fatcerberus
Copy link

Duplicate of #46254 and others. It's a known design limitation that all identifiers in a call expression must have explicit type annotations in order for the call to affect control flow, but the type of bar in your example is inferred (the : never return annotation applies to the arrow function expression itself, so doesn't count).

See #32695, which implemented the feature.

A function call is analyzed as an assertion call or never-returning call when

  • the call occurs as a top-level expression statement, and
  • 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 asserts return type or an explicit never return type annotation.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 12, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants