Skip to content

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

Closed
@TheBlue-1

Description

@TheBlue-1

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

fatcerberus commented on Jul 5, 2023

@fatcerberus

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

fatcerberus

fatcerberus commented on Jul 5, 2023

@fatcerberus
TheBlue-1

TheBlue-1 commented on Jul 5, 2023

@TheBlue-1
Author

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

fatcerberus

fatcerberus commented on Jul 5, 2023

@fatcerberus

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

guillaumebrunerie commented on Jul 6, 2023

@guillaumebrunerie

{} 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

TheBlue-1 commented on Jul 6, 2023

@TheBlue-1
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

guillaumebrunerie commented on Jul 6, 2023

@guillaumebrunerie

See #12936.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @guillaumebrunerie@fatcerberus@TheBlue-1

        Issue actions

          Return type union with empty object is just empty object Β· Issue #54888 Β· microsoft/TypeScript