Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function lacks ending return statement and return type does not include 'undefined'. in TypeScript

Tags:

typescript

I am studying TS. I'm trying to make simple calculator. How can I make it this calculator without switch statement? If the if statement is used, an error occurs in return type.

Screenshot of code

type Command = 'add' | 'substract' | 'multiply' | 'divide' | 'remainder';
const calculate = function (command: Command, num1: number, num2: number): number {
  if (command === 'add') return num1 + num2;
  if (command === 'substract') return num1 - num2;
  if (command === 'multiply') return num1 * num2;
  if (command === 'divide') return num1 / num2;
  if (command === 'remainder') return num1 % num2;

// switch (command) {
//  case 'add':
//      return num1 + num2;
//  case 'substract':
//      return num1 - num2;
//  case 'multiply':
//      return num1 * num2;
//  case 'divide':
//      return num1 / num2;
//  case 'remainder':
//      return num1 % num2;
//  default:
//      throw Error('unknown command');
// }
};
console.log(calculate('add', 1, 3)); // 4
console.log(calculate('substract', 3, 1)); // 2
console.log(calculate('multiply', 4, 2)); // 8
console.log(calculate('divide', 4, 2)); // 2
console.log(calculate('remainder', 5, 2)); // 1

TypeScript Playground

like image 642
김휘민 Avatar asked Oct 19 '25 03:10

김휘민


2 Answers

Typescript has a type checking paradigm as type inference and here it tries to find out what should happen for the return type if all your conditions won't match . You need to handle all return types, one of these solutions could be the bellow one:

type Command = 'add' | 'substract' | 'multiply' | 'divide' | 'remainder';
const calculate = function (command: Command, num1: number, num2: number): number | void {
  if (command === 'add') return num1 + num2;
  if (command === 'substract') return num1 - num2;
  if (command === 'multiply') return num1 * num2;
  if (command === 'divide') return num1 / num2;
  if (command === 'remainder') return num1 % num2;

// switch (command) {
//  case 'add':
//      return num1 + num2;
//  case 'substract':
//      return num1 - num2;
//  case 'multiply':
//      return num1 * num2;
//  case 'divide':
//      return num1 / num2;
//  case 'remainder':
//      return num1 % num2;
//  default:
//      throw Error('unknown command');
// }
};
console.log(calculate('add', 1, 3)); // 4
console.log(calculate('substract', 3, 1)); // 2
console.log(calculate('multiply', 4, 2)); // 8
console.log(calculate('divide', 4, 2)); // 2
console.log(calculate('remainder', 5, 2)); // 1
console.log(calculate("badcommand", 2, 4)) // this won't return any value just error or undefined

If you are trying to change the function implementation due to the error you can make all the calculation functions like:

type Calculation = (num1: number, num2: number) => number

const add: Calculation = (num1, num2) => {
    return num1 + num2;
}

const substract: Calculation = (num1, num2) => {
    return num1 - num2;
}
// Others
like image 156
Heartbit Avatar answered Oct 21 '25 03:10

Heartbit


If you replace the if/else chain with a switch statement, then Typescript will correctly determine that the end of the function is unreachable, and therefore doesn't need a return statement; on the contrary, if you try to put a return statement at the end, you will get an error for "unreachable code".

Note that there is no need for a default clause here, though you could add one if you want more safety at runtime.

const calculate = function (command: Command, num1: number, num2: number): number {
  switch(command) {
    case 'add':
      return num1 + num2;
    case 'substract':
      return num1 - num2;
    case 'multiply':
      return num1 * num2;
    case 'divide':
      return num1 / num2;
    case 'remainder':
      return num1 % num2;
  }
};

Playground Link

like image 43
kaya3 Avatar answered Oct 21 '25 04:10

kaya3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!