Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alex-vukov opened this issue May 9, 2024 · 2 comments
Closed
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@alex-vukov
Copy link

🔎 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

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label May 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

4 participants