Extending array prototype

Topics: General
Feb 26, 2013 at 9:46 AM
I found out that some people try to extend e.g. string class and that works fine - see http://stackoverflow.com/questions/13897659/extending-functionality-in-typescript.

But when I try to extend array prototype like this:
interface Array {
    addx() : bool;
}

Array.prototype.addx = function() { return true; };
then errors view in VisualStudio reports error at 'function() ...' saying

Error 24 Cannot convert '() => bool' to '{ (): number; (): number; (): number; (): number; (): number; (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }': Call signatures of types '() => bool' and '{ (): number; (): number; (): number; (): number; (): number; (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }' are incompatible c:\interfaces.ts 85 24 userPermissions.ts


Error 22 Cannot convert '() => bool' to '{ (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }': Call signatures of types '() => bool' and '{ (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }' are incompatible c:\interfaces.ts 85 24 inviteUser.ts

Error 20 Cannot convert '() => bool' to '{ (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }': Call signatures of types '() => bool' and '{ (): number; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; (): bool; }' are incompatible c:\interfaces.ts 85 24 api.ts

etc.

When I run tsc compiler, it runs fine.
How can I avoid the errors in VS error output?
Coordinator
Feb 26, 2013 at 7:38 PM
This is likely due to the fact that Array is treated specially in the 0.8.x series because of the lack of generics. As we add generics to the language, special cases like this (with their limitations) should go away.
Feb 27, 2013 at 11:34 AM
Oh, that's good to hear! When can we expect generics in typescript?
Coordinator
Feb 27, 2013 at 4:39 PM
Indeed. We're hoping publish a draft of the spec with generics for the community to comment on soon. Also, our experimentations on generics and speeding up the compiler are also available in the develop-0.9.x branch. Still early on, but may be worth a look.
Coordinator
Mar 1, 2013 at 3:56 PM
We'll probably post a blog soon talking about the design, but you can grab a sneak peek now at the generics design we're proposing in the draft spec that's in the develop-0.9.x branch's doc folder.