Skip to content

'return' statement required after function that returns 'never' #10470

Closed
@jovdb

Description

@jovdb

TypeScript Version: 2.1.0-dev.20160819

Code

// Type representing the falsy values
type Falsy = null | undefined | false | 0 | ''; // No NaN type

function assert(value: Falsy, message: string): never;
function assert<T>(value: T, message: string): T;
function assert<T>(value: Falsy | T, message: string): never | T {
  if (!value) throw new Error(`AssertError: ${message}`);
  return value;
}

function foo(bar: number): string {

  // Unimportant
  if (bar === 0) return 'off';
  if (bar > 1) return 'on';

  assert(false, 'Invalid value for bar'); // because of 'false' argument, this will have return type 'never'

  // Error: Function lacks ending return statement and return type does not include 'undefined'
}

Expected behavior:
I would expect if the type system knows that assert(false, '') never returns, it doesn't require a return statement after assert.

Actual behavior:
Error on foo function:
Function lacks ending return statement and return type does not include 'undefined'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedQuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions