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

TS 5.8: False positives for conditional expression in return type check #61337

Open
AlCalzone opened this issue Mar 3, 2025 · 1 comment
Open
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@AlCalzone
Copy link
Contributor

πŸ”Ž Search Terms

5.8, return, type

πŸ•— Version & Regression Information

  • This changed between versions 5.7.3 and 5.8.2

⏯ Playground Link

No response

πŸ’» Code

export type ReturnTypeOrStatic<T> = T extends (...args: any[]) => infer R ? R
	: T;

export function evalOrStatic<T>(
	fnOrConst: T,
	...args: any[]
): ReturnTypeOrStatic<T> {
	return typeof fnOrConst === "function" ? fnOrConst(...args) : fnOrConst;
}

πŸ™ Actual behavior

Error under fnOrConst at the end of the conditional:

Type 'T' is not assignable to type 'ReturnTypeOrStatic<T>'

πŸ™‚ Expected behavior

no error. If fnOrConst is a function, its return value (type R) is returned, otherwise it is returned directly (type T).

Additional information about the issue

No response

@Andarist
Copy link
Contributor

Andarist commented Mar 3, 2025

If fnOrConst is a function, its return value (type R) is returned, otherwise it is returned directly (type T).

This is true but TS doesn't have a concept of negated types today so it can't quite reason about the "else" branch of this.

This is an expected breaking change. It was announced here. Note that your code has never worked in a similar situation (that logical-wise is actually the same):

export type ReturnTypeOrStatic<T> = T extends (...args: any[]) => infer R
  ? R
  : T;

export function evalOrStatic2<T>(
  fnOrConst: T,
  ...args: any[]
): ReturnTypeOrStatic<T> {
  if (typeof fnOrConst === "function") {
    return fnOrConst(...args);
  }
  return fnOrConst; // error
}

You can see it on the playground here

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants