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 of return type void in interface can return anything #9603

Closed
sant123 opened this issue Jul 10, 2016 · 3 comments
Closed

Function of return type void in interface can return anything #9603

sant123 opened this issue Jul 10, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@sant123
Copy link

sant123 commented Jul 10, 2016

TypeScript Version: 1.8.10

Hi, I have this code and as you can see the function greet has a return type of void.

Code

interface Person {
    name : string;
    lastName : string;
    greet : (person : this) => void;
}

declare var some : Person;

var obj : Person = {
    name : "Santi",
    lastName : "Aguilar",
    greet : function(person) {
        return "some string";
    }
}

Expected behavior:

Type string is not assignable to type void.

Actual behavior:

No error.

However, if you explicitly specify the return type void in the implementation; it works!

var obj : Person = {
    name : "Santi",
    lastName : "Aguilar",
    greet : function(person) : void {
        return "some string"; //Type string is not assignable to type void.
    }
}
@zpdDG4gta8XKpMCd
Copy link

cpnsider your vote to fix: #8584

@zpdDG4gta8XKpMCd
Copy link

@RyanCavanaugh, this what rationale applies here? (doesn't seem to be the case of #8581 (comment))

@kitsonk
Copy link
Contributor

kitsonk commented Jul 11, 2016

There is no bottom type until TypeScript 2.0. Therefore void in an interface is essentially saying "you should ignore a return value from a function. But just like excess parameters, TypeScript evaluates if the resulting object can be conformed to the shape of the interface. So the below is valid (although a bit surprising):

interface Foo {
    foo(a: string, b: number): void;
}

const foo: Foo = {
    foo() {
        return 'bar';
    }
};

But then of course if you were to depend upon the return type TypeScript would get upset:

function bar(c: string) {
    console.log(c);
}

bar(foo.foo()); // error here

In TypeScript 2.0 we get the bottom type never, which is a hard "this will NEVER return a value" versus a soft "don't depend upon any returned value" and some more strictness around unused parameters which will allow us to enforce these contracts more specifically.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Jul 11, 2016
@mhegazy mhegazy closed this as completed Jul 11, 2016
@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants