Is this a bug of the typescript compiler? Higher-order function using union types

41 Views Asked by At
function toNumber(input: 'a' | 'b'): number {
  switch (input) {
    case 'a':
      return 1;

    case 'b':
      return 2;
  }
}

function bar(toNumber: (input: 'a' | 'b' | 'c') => number): number {
  return toNumber('c');
}

bar(toNumber); // No typescript error, but returns `undefined`

I expect TypeScript to report an error the last line. There's clearly a type error there.

Is this a bug of the typescript compiler? If so, can you link me to an open issue in the compiler repo or should I create one?

1

There are 1 best solutions below

0
mauroc8 On

Typescript only ignores this error when the option strict is set to false.

(Thanks @andy-ray for pointing that out in the comments)