【问题标题】:Is there a way to use Typescript.Collections.HashTable in my code?有没有办法在我的代码中使用 Typescript.Collections.HashTable?
【发布时间】:2013-09-11 21:41:55
【问题描述】:

我在 Typescript 编译器的代码中看到“HashTable”的实现(在文件 src/compiler/core/hashTable.ts 中)。

你知道有什么方法可以直接在我的 Typescript 项目中使用吗?

【问题讨论】:

  • 你有没有得到这个工作?如果是这样,你做了什么?

标签: typescript


【解决方案1】:

我对那些试图重新发明轮子的人感到厌烦。

Typescript 是 Javascript 的超集,这意味着任何 Javascript 都可以正常工作。

在 Javascript 中,您有 Map(),它不是 100% 像哈希表,但有类似的用法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections

需要注意的一点是,这里的 Map 实现允许您多次设置同一个键,它会覆盖旧值。

我为避免这种情况而制作并使用的一个功能如下:

private add<K,V>(map:Map<K,V>, key:K, value:V){
    if(map.has(key)){
        throw new TypeError("Key "+ key +" already exists!");
    }else{
        map.set(key,value);
    }
}

我通过执行以下操作将我之前定义的地图传递给它:

MyMap = new Map();

MyMapStrict = new Map<string,string>();

然后传递一个必须尊重映射键和值类型的键和值。否则 Typescript 编译器会报错。

例子:

add(MyMapStrict, "myKey", "myvalue");

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您可以通过定义一个接口来实现一个非常简单的哈希表,其中键是一个字符串

    class Person {
        name: string;
    }
    
    interface HashTable<T> {
        [key: string]: T;
    }
    
    var persons: HashTable<Person> = {};
    persons["bob"] = new Person();
    var bob = persons["bob"];
    

    它只能键入字符串或数字。

    【讨论】:

    • 如果我们必须键入一个更复杂的对象而不是字符串或数字,这个过程会更困难/冗长吗?你会认为 TypeScript 会附带一个内置的字典数据结构......
    • 好吧,typescript 没有附带任何东西,它只是编译时检查。所以我们任由 javascript 摆布。
    • 你不必为它创建一个完整的 HashTable 类型:var persons: { [key: string]: Person } = {};
    【解决方案3】:

    下载文件“hashTable.ts”并将其放在您的文件旁边。然后在文件顶部执行:

    ///<reference path='hashTable.ts' />
    

    PS:我会推荐一个 look at a lib TypeScript Generic Collections 我创作的。这是一个字典示例:

    class Person {
        constructor(public name: string, public yearOfBirth: number,public city?:string) {
        }
        toString() {
            return this.name + "-" + this.yearOfBirth; // City is not a part of the key. 
        }
    }
    
    class Car {
        constructor(public company: string, public type: string, public year: number) {
        }
        toString() {
            // Short hand. Adds each own property 
            return collections.toString(this);
        }
    }
    var dict = new collections.Dictionary<Person, Car>();
    dict.setValue(new Person("john", 1970,"melbourne"), new Car("honda", "city", 2002));
    dict.setValue(new Person("gavin", 1984), new Car("ferrari", "F50", 2006));
    console.log("Orig");
    console.log(dict);
    
    // Changes the same john, since city is not part of key 
    dict.setValue(new Person("john", 1970, "sydney"), new Car("honda", "accord", 2006)); 
    // Add a new john
    dict.setValue(new Person("john", 1971), new Car("nissan", "micra", 2010)); 
    console.log("Updated");
    console.log(dict);
    
    // Showing getting / setting a single car: 
    console.log("Single Item");
    var person = new Person("john", 1970); 
    console.log("-Person:");
    console.log(person);
    
    var car = dict.getValue(person);
    console.log("-Car:");
    console.log(car.toString());
    

    【讨论】:

    • 这仍然是类型哈希表的最佳解决方案吗?或者他们现在在核心打字稿中有类似的东西吗?谢谢。
    • @DavidThielen 对迟到的通知感到抱歉:TypeScript 不附带 standard 库(如 .net 的 BCL)。
    【解决方案4】:

    根据this 博客文章,TypeScript 定义了一个Map 接口,看起来类似于HashTable

    其他收藏

    • 弱地图
    • 设置
    • 弱集

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2016-12-11
      相关资源
      最近更新 更多