Closed
Description
Bug Report
π Search Terms
conditional return
return type option missing
only first return type
empty object return
π Version & Regression Information
- This is the behavior in version 5.1.3 and nigtly 5.2.0-dev.20230705
β― Playground Link
Playground link with relevant code
π» Code
type Expressions={isCoolExpression:true}
type ActionStep = {
input?: Expressions | undefined;
}
export function evaluateInput(step: ActionStep) {
if (!step.input) return {};
return 1;
}
const u=evaluateInput(null as any); // u's type is "{}" instead of "{} | 1"
π Actual behavior
If a function returns either an empty object or a number (could also be or a promise or anything) the return type will be just empty object instead of a union type. It is wrong because a number or promise doesnt equal to an empty object. A promise is much more than an empty object and it should be typed that way, a number is just totally different in usage and should therefore be typed with a union.
π Expected behavior
The expected behaviour would be a return type as a union like "1 | {}" or "{} | Promise"
Activity
fatcerberus commentedon Jul 5, 2023
This is just normal subtype reduction. Everything (except
null
&undefined
) is a subtype of{}
.fatcerberus commentedon Jul 5, 2023
Duplicate of #50171, #49710, and likely others.
Also see: https://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type
TheBlue-1 commentedon Jul 5, 2023
But it is a mistake to do the subtype reduction for infered return types, because it hides the actual returns.
fatcerberus commentedon Jul 5, 2023
The inferred return type here is no different in concept to the inferred type of the ternary expression in #50171. It's inconvenient, but ultimately not considered a bug.
guillaumebrunerie commentedon Jul 6, 2023
{}
does not mean "empty object", it means anything with any properties you want, or in other words any value exceptnull
andundefined
. In particular, every number is already assignable to the type{}
, so even if it did return1 | {}
, there would be no way to make use of this information.TheBlue-1 commentedon Jul 6, 2023
closing since ts wont solve the problem... There should be a type saing its just an empty object or sth else and not a "base type" that just includes everything since its just wrong...
guillaumebrunerie commentedon Jul 6, 2023
See #12936.