3

Closed

Inline function not getting checked for type safety

description

I am new to typescript and trying few things

where function demands number parameter and bool as return value.
function where(items: number[], filter: (item: number) => bool) {
    for (var counter = 0; counter < items.length; counter++) {
        var item = items[counter];
        if (filter(item)) {
            console.log(item);
        }
    }
}
var data = [1, 2, 3, 4];
Case 1 - TypeScript gives a compile error
var evenFilter = function (item: number) {
    //actual line is item % 2 == 0;
    return item;
}
where(data, evenFilter);
Case 2 - Inline function with incorrect return type does not generate compile time error
where(data, function(item : number){
    return item;
});
Case 3 - Even the short hand syntax does not generate compile time error
where(data, item => item);
Is this a bug with TypeScript or expected behavior?
Closed Jun 13, 2013 at 6:26 PM by RyanCavanaugh
Fixed in develop branch

comments

Grajkowski wrote May 9, 2013 at 3:23 PM

It's definitely weird that Case 1 and 2 aren't functionally the same.

danquirk wrote May 10, 2013 at 7:57 PM

Both Case 1 and Case 2 give correct, and equivalent, errors in the latest develop bits for 0.9. You're correct that it is a bug in 0.8.3 that Case 2 is not the same error. You defined where's 2nd parameter as a function that takes a number and returns a bool. Your evenFilter variable is not of that type, it is number => number. In Case 2 this is exactly the same, you've simply used an anonymous function with the same type signature.

Case 3 is a bug that still exists in 0.9. marking this as active for investigation.