Question

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!

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top