microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Home Page:https://www.typescriptlang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when using different types of return which should have the same result

alex-vukov opened this issue Β· comments

πŸ”Ž Search Terms

short circuit return error, ternary return error

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about short-circuit return error or ternary return error

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/MYewdgzgLgBKYDMCWBzGBeGBvAUDGADgE5IC2AhkQJ4BiArmMFEuBAFzZ74wDkCDTFmACCPDv0bNwwgDRd8fAVLAAhMTAmDwKufgC+umBACm8ACaVaSoe07dem5QGF1joU8N6cegNw4cbuAa1tIAFNBEHBFIYCgAlHYwRMZQdERgRlBEMADUvKJ+XgEhGYGqoWB0pByVpABGxkQJuPjJqelw5AA2XQDKpuAW1PSSQqGKo+AuMjBZdMYJeTxqhf5lwZNgTqF1ICBdHLv7xuRgzVzw0DAAHhgwAIT3wN1dAAokFMMl42WiMzxQYzQHhxPxcAD04JgThApFIxjAsCgAAskBAYHUuiBgABrGCnMwwASw+GI-FgKgwEAIWbI4xUlGNdENLEAdzYXCQNNC13O9jaaQyRy6fn0MGMXRMSRSgo03RMYPwkIFHVuAH4MXsujBxPLjKKYJDpe0MrcAGRmzX7bz+S6wZ49d5kSwjLQZTAAHgAKuLroCwGZ0TjjFRqbMqARjGH4MgUAA6YjOr6bCAAPlCZQAcuR4RwvTM44XKChbK9KDmUkyPVAI1GaTHUAmPi6ShAANpegC6qYS6FTiRVGVCDfjic+VhTbazFc7+PRpyocVChbjxYgoJtODtnR6-XMLc2d29vv9gZgwdDNJrkej4FjcZM++TbrTGZK2dzMHzMBXa44ZaICtASICBq1rW9EEbR9BgPF8O27Xt+xaY1ZWHO8oIGANYOUdtp3hWdyHnCkl1-IgSw3IpLmOOMsRQYcXidcdXWUH4Sj+XhAWBOINyorpjBokA6IdN5m2fFiJjdNQZgAVm4vwgA

πŸ’» Code

const config = {
  primaryFunctions: {
    'functionA': functionA,
    'functionB': functionB,
  },
  secondaryFunctions: {
    'functionC': functionC,
  }
};

function functionA(str: string) {
  return str + 'A';
}

function functionB(num: number) {
  return callSecondaryFunction('functionC', true) + 'B';
}

function functionC(bool: boolean) {
  const x = !!callPrimaryFunction('functionA', 'test');

  // Comment this block and uncomment any of the others below:
  if (x) {
    return bool;
  } else return false;

  //return x ? bool : false;
  // return x && bool
}

const callPrimaryFunction = <T extends keyof typeof config.primaryFunctions>(functionName: T, ...args: Parameters<typeof config.primaryFunctions[T]>) => {
  return (config.primaryFunctions[functionName] as any)(...args);
}

const callSecondaryFunction = <T extends keyof typeof config.secondaryFunctions>(functionName: T, ...args: Parameters<typeof config.secondaryFunctions[T]>) => {
  return (config.secondaryFunctions[functionName] as any)(...args);
}

console.log(callPrimaryFunction('functionA', 'test'));
console.log(callPrimaryFunction('functionB', 5));

πŸ™ Actual behavior

When you go to functionC comment the following block

if (x) {
    return bool;
} else return false;

and uncomment any of the other two below:

  //return x ? bool : false;
  // return x && bool

you will get 'callPrimaryFunction' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.(7023) errors everywhere even though the code should work in an identical way.

πŸ™‚ Expected behavior

  if (x) {
    return bool;
  } else return false;

or

return x ? bool : false;

or

return x && bool

should behave in the same way and not cause errors because they return the exact same result in Javascript.

Additional information about the issue

No response