This project is read-only.

How to API to Array var in lib.d.ts

Topics: General
May 30, 2014 at 4:40 PM
I'm trying to add an API to the declare var Array in the lib.d.ts, but when I compile it throws and error referencing my ususage in my TS files saying that they property doesn't exist. Is it possible to add APIs to the lib.d.ts. I closed VS and reopened so that intellisense recognized the API, but it wouldn't compile.


declare var Array: {
from<T>(obj: any): T[];

my TS file
var arr = Array.from<HTMLElement>(document.querySelectorAll("div"));
May 30, 2014 at 6:41 PM
Edited May 30, 2014 at 6:41 PM
No, unfortunately you cannot redefine an existing variable.
May 30, 2014 at 6:47 PM
You can, however, add methods to the INSTANCE (not the static var), like so:
interface Array<T> {
    from<T>(obj: any): T[];
May 30, 2014 at 7:02 PM
Thanks for the response, but I was attempting add a polyfill the would be forward compatible w/ ES6. Do you guys have any plans on allowing at least the merging of declare var's? I understand by you don't for actual var's, but since you can easily extend objects/types in JS, merging of declare var's should be allowed?
May 30, 2014 at 7:34 PM
Edited May 30, 2014 at 7:36 PM
An issue was recently posted because of this limitation:

So far, I would suggest considering putting all your code in your own namespace, then you will be able to provide your own definition, like so:
module myApp {
    declare var Array: {
        new (arrayLength?: number): any[];
        new <T>(arrayLength: number): T[];
        new <T>(...items: T[]): T[];
        (arrayLength?: number): any[];
        <T>(arrayLength: number): T[];
        <T>(...items: T[]): T[];
        isArray(arg: any): boolean;
        prototype: Array<any>;
        from<T>(obj: any): T[];
    // ... the reset of your app ...
(TIP: I Just copied the lib.t.ds definition, and added to it.)
May 30, 2014 at 7:38 PM
FYI: Somewhat related post (for reference):