Skip to content

Inferred return type is incompleteΒ #43331

Closed
@jakubnavratil

Description

@jakubnavratil

Bug Report

πŸ”Ž Search Terms

Infer, union, return type

πŸ•— Version & Regression Information

  • Tested on 3.8.2 - 4.2.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type TestA = { foo: boolean };
type TestB = { foo: boolean; bar: number }; // diffrence in exta prop

const fn = (val1: TestA, val2: TestB) => {
  if (Math.random() > 0.5) {
    return val1;
  }

  return val2;
};

πŸ™ Actual behavior

fn return type is TestA

πŸ™‚ Expected behavior

fn return type is TestA | TestB

This happens when TestB have some similarity with TestA. If I remove foo from TestB then it works ok.
This is simplified example. What I want is union of all posible types.

Or is there other way how to extract that union type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions