57

Did they add a runtime List<> and/or Map<> type class to typepad 1.0? And if not, is there a solid library out there someone wrote that provides this functionality?

And in the case of List<>, is there a linked list where the elements in the list have the next/prev property? We need a list where from an element object (ie not from an iterator), we can get the next and previous elements in the list (or null if it's the first/last one).

3
  • 2
    Typescript is just an extension of JavaScript, compiled to Javascript, that adds data-types and some features that are "coming soon" to JavaScript. It has no run-time at all. Commented Apr 16, 2014 at 0:14
  • I believe Angular did what you're looking for: Collection.ts Commented May 18, 2015 at 21:15
  • Take a look at Script4J that is a set of libraries to write programs in TypeScript/JavaScript using Java API. Although it is a new project it is very interesting. Commented Nov 15, 2018 at 21:31

2 Answers 2

72

It's very easy to write that yourself, and that way you have more control over things.. As the other answers say, TypeScript is not aimed at adding runtime types or functionality.

Map:

class Map<T> {
    private items: { [key: string]: T };

    constructor() {
        this.items = {};
    }

    add(key: string, value: T): void {
        this.items[key] = value;
    }

    has(key: string): boolean {
        return key in this.items;
    }

    get(key: string): T {
        return this.items[key];
    }
}

List:

class List<T> {
    private items: Array<T>;

    constructor() {
        this.items = [];
    }

    size(): number {
        return this.items.length;
    }

    add(value: T): void {
        this.items.push(value);
    }

    get(index: number): T {
        return this.items[index];
    }
}

I haven't tested (or even tried to compile) this code, but it should give you a starting point.. you can of course then change what ever you want and add the functionality that YOU need...

As for your "special needs" from the List, I see no reason why to implement a linked list, since the javascript array lets you add and remove items.
Here's a modified version of the List to handle the get prev/next from the element itself:

class ListItem<T> {
    private list: List<T>;
    private index: number;

    public value: T;

    constructor(list: List<T>, value: T, index: number) {
        this.list = list;
        this.index = index;
        this.value = value;
    }

    prev(): ListItem<T> {
        return this.list.get(this.index - 1);
    }

    next(): ListItem<T> {
        return this.list.get(this.index + 1);   
    }
}

class List<T> {
    private items: Array<ListItem<T>>;

    constructor() {
        this.items = [];
    }

    size(): number {
        return this.items.length;
    }

    add(value: T): void {
        this.items.push(new ListItem<T>(this, value, this.size()));
    }

    get(index: number): ListItem<T> {
        return this.items[index];
    }
}

Here too you're looking at untested code..

Hope this helps.


Edit - as this answer still gets some attention

Javascript has a native Map object so there's no need to create your own:

let map = new Map();
map.set("key1", "value1");
console.log(map.get("key1")); // value1
Sign up to request clarification or add additional context in comments.

11 Comments

I just realized, your final program won't work if items are inserted/removed from the list as the index value for that element will then be different.
Of course, as I wrote, it's a simple code to get you started, you'll need to implement that kind of thing, just like with an iterator
no remove method ?
@baraber Well, as I wrote it's just an example of how to implement things yourself. Adding a remove method is pretty trivial. I can edit and add it one, to which class are you referring to?
@IamStalker The Map class I created uses a js object as the underlying data structure, and all keys in js objects are strings, which is why my class only has one generic constraint for the value. The es6 Map object does support other types for the keys, which is why it has two generic constraints in the definition files.
|
27

Did they add a runtime List<> and/or Map<> type class to typepad 1.0

No, providing a runtime is not the focus of the TypeScript team.

is there a solid library out there someone wrote that provides this functionality?

I wrote (really just ported over buckets to typescript): https://github.com/basarat/typescript-collections

Update

JavaScript / TypeScript now support this natively and you can enable them with lib.d.ts : https://basarat.gitbook.io/typescript/type-system/lib.d.ts along with a polyfill if you want 🌹

8 Comments

One note on using this - if using requireJS you need to also have the file explicitly loaded before you use it.
Hi again. FYI your collections (which I like) can't be used in objects passed to/from web workers. The LinkedList will drop all objects it contains. And the Dictionary will throw an exception as not serializable.
They don't pass - sometimes. Very weird where the list comes across populated in some cases, empty in others. I'm trying to figure out why.
Hi again. Ok, Dictionary.toStr is the problem. If I set that to null or undefined in the debugger before the postMessage call, then it's all ok. But if that is the default function - can't post.
What is buckets?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.