3

Closed

Arrays do not implement { length: number; }

description

If you create an interface with a field named length of type number, then arrays (any[], string[], etc.) do not implement it, even though they have that field. (Strings, Typed Arrays, and plain objects with the field do work properly, though.)
function isEmpty(list: { length: number; })
{
    return list.length === 0;
}

console.log(isEmpty("")); // true
console.log(isEmpty("foo")); // false
console.log(isEmpty({ length: 0 })); // true
console.log(isEmpty({ length: 4 })); // false
console.log(isEmpty(new Uint8Array(0))); // true
console.log(isEmpty(new Uint8Array(3))); // false
console.log(isEmpty([])); // Should be true, but there's a compiler error.
console.log(isEmpty([0, 1, 2])); // Should be false, but there's a compiler error.
(This was tested in the online TypeScript Playground.)
Closed Apr 5, 2013 at 9:08 PM by paulb
This is fixed in our develop branch and will be part of the 0.9x release.

comments

paulb wrote Jan 30, 2013 at 8:52 PM

Thanks for reporting this. I marked it as active which means we're taking a look at fixing this.

jopamer wrote Feb 18, 2013 at 11:19 PM

Also, as an aside - we're currently working on the fix for this bug, but in the meantime you can work around the issue by including a type assertion to "any" on your argument expression.