Skip to content
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

Function's return type does not varies based on its arguments. (with overloads) #13258

Closed
xLama opened this issue Jan 3, 2017 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@xLama
Copy link

xLama commented Jan 3, 2017

TypeScript Version: 2.1.14

Code

function foo(name: number): number;
function foo(name: string): string;
function foo(name: string | number): string | number {
    if (typeof name === "number") {
        name; // name is number here
        return "foo"  // ¿?
    } else {
        name; // name is string here
        return 10 // ¿?
    }
}

let bar = foo("foo"); // Return a string but it is a number 

Expected behavior:

Error on return "foo" and return 10 becasue return types does not match with their signatures.

Actual behavior:

No errors.

Reading specs we can find this:

Thus, using overloads it is possible to statically describe the manner in which a function's return type varies based on its arguments.

https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#6.2

Worked as intented, maybe?

@xLama xLama changed the title Function's return type does not varies based on its arguments. Function's return type does not varies based on its arguments. (with overloads) Jan 3, 2017
@DanielRosenwasser
Copy link
Member

Right now there's no verification between any overload signatures and the implementation itself. Check out #13235 which requests this sort of functionality.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jan 3, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants