Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Why is this not a type error?

Tags:

typescript

I'm trying to understand the degree of type inference at work in TypeScript. In the following code example, why is foo's implementation of baz.esplode valid? My understanding is that an empty method matches void.

interface bar {
    horace: number;
}

interface baz {
    esplode: (string, number) => bool;
}

interface bazzer extends bar, baz { }

var foo: bazzer = {
    horace: 12,
    esplode: function () { }
}

var x = foo.esplode('crackers', 2);

Thanks!

like image 218
Jason Suárez Avatar asked Oct 02 '12 08:10

Jason Suárez


1 Answers

thanks for taking a look!

That's actually a bug. When a function is contextually typed, we should treat it as though a return type annotation exists that represents the intended return type (per section 4.9 of the language spec), so you're right in that there should be an error.

I already have a fix for this, but can you file a bug on the CodePlex site so our team can track it? I can push the fix to our develop branch this afternoon.

Thanks again!

like image 127
Joe Pamer Avatar answered Oct 06 '22 15:10

Joe Pamer