norekhov norekhov -4 years ago 212
TypeScript Question

How to use collections.js in typescript?

I want to use collections.js in my typescript Angular2 project. I want to start with multimap. There's no @types/collections package and there's no collections.js in typings.

How can I create my own multimap.d.ts file in my project without putting it into collections folder?

Even when I write

const MultiMap = require('collections/multi-map')

I got "NoProviderError for PlatformRef" in Agnular2. Well I know there are "typings" though I suspect it will copy the file into node_modules directory, right?


I think that's important that I have:

// support NodeJS modules without type definitions
declare module '*';

In my custom-typings.d.ts

Answer Source

An error NoProviderError is caused by the fact that collection.js library changes the way how built-in primitives work, particularly Array.

Internally, zone.js (which is a part of Angular) invokes Array.from with an instance of MapIterator as an argument. In collections/shim-array.js file, this library overrides Array.from with an implementation, that doesn't work with MapIterator. In screenshot you can see, that it returns an empty array, while native implementation returns an array of 13 elements.

enter image description here

A conclusion is, that sadly you can't use Collections.js library with Angular.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download