Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a typescript List<> and/or Map<> class/library? [closed]

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).

like image 861
David Thielen Avatar asked Apr 15 '14 23:04

David Thielen


People also ask

Is there list in TypeScript?

There is no built-in list type in TypeScript; however, TypeScript provides the Array type for storing contiguous data elements. It is easy to create a list data structure ADT using the Array type.

Does TypeScript have ArrayList?

There is no ArrayList in javascript.

What does ?: Mean in TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

How do I create a list of objects in TypeScript?

To declare an array of objects in TypeScript, set the type of the variable to {}[] , e.g. const arr: { name: string; age: number }[] = [] . Once the type is set, the array can only contain objects that conform to the specified type, otherwise the type checker throws an error. Copied!


1 Answers

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 
like image 88
Nitzan Tomer Avatar answered Oct 13 '22 13:10

Nitzan Tomer