Typescript: `Property 'x' does not exist on type 'any[]'.` when exporting class.

Created on 25 Apr 2016  Â·  7Comments  Â·  Source: microsoft/TypeScript

Hi guys!

We ran into a strange behaviour on StackOverflow regarding the typescript compiler. We were trying to extend the Array interface, and it worked well until we exported something. I don't know what is the expected behavior when extending builtin types interfaces, sorry if I am wrong here, but as I see a seemingly unrelated export should not affect the validity of that.

The report originates from: http://stackoverflow.com/questions/36805280/how-to-add-tolist-method-into-typescript-arrayt-class/36810621?noredirect=1#comment61231653_36810621

TypeScript Version:

Whatever version was published on the Playground (https://www.typescriptlang.org/play/index.html) on 2016.04.25. (I couldn't find it out)

Code

interface Array<T> {
    ToList():List<T>;
};

class List<T> {
    constructor(items: T[]) {}
};

Array.prototype.ToList = function() {
    return new List(this);
};

export default "anything";

Expected behavior:

Should compile. The strange thing is that when I remove the export the error vanishes.

Actual behavior:

Compilation error is shown on Array.prototype.ToList =: Property 'ToList' does not exist on type 'any[]'.

Thanks

Docs Question

Most helpful comment

Need to add "Why does adding export break my interface extensions" to the FAQ

All 7 comments

The strange thing is that when I remove the export the error vanishes.

Adding a root level export makes the file a module. This disconnects Array<T from the global `Array. https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Proper way to add to global Array is to use a something.d.ts file specific for this or use declare global both of which are covered here : https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html :rose:

Thanks, I felt we were missing something, now I see. Sorry for spamming
you, this issue can now be closed.

Have a nice day
On Apr 25, 2016 6:07 AM, "Basarat Ali Syed" [email protected]
wrote:

The strange thing is that when I remove the export the error vanishes.

Adding a root level export makes the file a module. This disconnects Array from the global `Array.
https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Proper way to add to global Array is to use a something.d.ts file specific
for this or use declare global both of which are covered here :
https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/Microsoft/TypeScript/issues/8278#issuecomment-214120367

is this correct? this is working fine for me. Please let us know the best practice to extend the existing type?

declare global {
    interface Array<T> {
        ToList(): List<T>;
    }
}

Array.prototype.ToList = () => new List(this);`

export class List<T> {
    constructor(private array: T[] = []) {}

     public Add(item: T): List<T> {
        this.array.push(item);
        return this.array.ToList();
    }
}

Array.prototype.ToList = () => new List(this);`

@natarajanmca11 you definitely have the wrong this here. Don't use an arrow function in this case https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html :rose:

Need to add "Why does adding export break my interface extensions" to the FAQ

is it possible ot make the extension type safe?

for example:

declare global {
  interface Array<T extends Coordinate> {
    toPoints(): string;
  }
}

?

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlaberge picture dlaberge  Â·  3Comments

jbondc picture jbondc  Â·  3Comments

siddjain picture siddjain  Â·  3Comments

blendsdk picture blendsdk  Â·  3Comments

remojansen picture remojansen  Â·  3Comments