Skip to content

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

Closed as not planned
@yyijoo

Description

@yyijoo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions