1

Given a nested overloaded function, such as innerOverloaded below, what is the best way to call it from a parent overloaded function that has similarly overloaded parameters as the inner one, as in the case with overloadedWithError?

It seems that TypeScript can't figure out that the types are covariant (using the term somewhat loosely here).

function innerOverloaded(foo: true): false;
function innerOverloaded(foo: false): true;
function innerOverloaded(foo: boolean): boolean {
  return !foo;
}

function overloadedWithError(foo: boolean): boolean {
  return innerOverloaded(foo);
}

function overloadedButUgly(foo: boolean): boolean {
  return foo ? innerOverloaded(foo) : innerOverloaded(foo);
}

function overloadedButLessUgly(foo: boolean): boolean {
  return innerOverloaded(foo as true);
}
0

1 Answer 1

3

boolean is a union true | false, as any union TS needs something to narrow it down.

There is an open ticket about this : #14107.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.