Skip to content

Function Overload Parameter and Return Mismatch #23861

Closed
@MrDesjardins

Description

@MrDesjardins

TypeScript Version: 2.9.0-dev.20180503
Search Terms: overload, function, parameter, return

Code

function funct5(param1: boolean): string;
function funct5(param1: Date): number;
function funct5(param1: boolean | Date): string | number {
        if (typeof param1 === "boolean") {
            return 1; // Expected to have a TS error saying that only a string is valid
        } else {
            return 0;
        }
}

Expected behavior:
Since the param1 is narrowed down to be a boolean, only a single overload is valid (the first one that returns a string). However, I can return a number which should only be valid if the param1 is a Date.

Actual behavior:
I was expecting TypeScript to notify me that the return type was invalid because of the type of the param1.

Playground Link: Link

Related Issues:

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