Skip to content

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

Closed as not planned
@AlCalzone

Description

@AlCalzone

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions