Skip to content

Return type union with empty object is just empty object #54888

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

Closed
TheBlue-1 opened this issue Jul 5, 2023 · 7 comments
Closed

Return type union with empty object is just empty object #54888

TheBlue-1 opened this issue Jul 5, 2023 · 7 comments

Comments

@TheBlue-1
Copy link

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"

@fatcerberus
Copy link

This is just normal subtype reduction. Everything (except null & undefined) is a subtype of {}.

@fatcerberus
Copy link

fatcerberus commented 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
Copy link
Author

But it is a mistake to do the subtype reduction for infered return types, because it hides the actual returns.

@fatcerberus
Copy link

But it is a mistake to do the subtype reduction for infered return types

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
Copy link

{} does not mean "empty object", it means anything with any properties you want, or in other words any value except null and undefined. In particular, every number is already assignable to the type {}, so even if it did return 1 | {}, there would be no way to make use of this information.

@TheBlue-1
Copy link
Author

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
Copy link

See #12936.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants