Skip to content

Inferred return type of function is incorrect when one of the return types is a conditional typeΒ #49301

@jespertheend

Description

@jespertheend

Bug Report

πŸ”Ž Search Terms

ignored return type generic conditional

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about return

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Foo<T> = {
    template: T;
}

type Bar<T> = T extends boolean ? Foo<number> : Foo<string>;

type Baz<T extends boolean> = Bar<T> extends infer InferredFoo
    ? InferredFoo extends Foo<any>
        ? "yes"
        : never
    : never;

function test<T extends boolean>(opts: T) {
    const x = false;

    // If you comment the line below, this function starts
    // returning Baz<T> as expected, which in turn makes `result`
    // have the type "yes".
    // The if statement below should have no impact to the
    // return type of the function, other than adding `| null`.
    // After all, you can force the return type of the function
    // to be `Baz<T> | null` and it still works fine.
    if (x) return null;

    return {} as Baz<T>;
}

const result = test(true);

πŸ™ Actual behavior

result has type null

πŸ™‚ Expected behavior

result should have type null | "yes"

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional types

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions