Skip to content

Return type of a function that calls an error throwing function #13625

Closed
@anilanar

Description

@anilanar

TypeScript Version: 2.1.1

Code

function throwError(message: string): void {
  throw new Error(
    preprocess(message)
  );
}

// this function causes a compile error, obviously
// because it doesn't return a number
// but throwError surely throws an error so it doesn't matter
function foo(): number {
  throwError('message');
}

My use case is I'm writing some library code, where there's an interface as follows:

interface IFoo {
  foo(): number;
}

There are some modules implementing this interface, however foo is an optional method thus not all implementations will have foo method. When someone calls foo, instead of causing a Javascript error undefined is not a function, I'd prefer to throw a more descriptive error, preferably using throwError function.

I assume this would be tricky to implement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn 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