- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果我想在 TypeScript 中使用带有类型检查的 string:object
字典,我知道有两种方法可以做到这一点:
const m = new Map<string, MyObject>();
m.set("a", new MyObject("a"));
m.set("b", new MyObject("b"));
和
const m : {[index: string]: MyObject} = {};
m["a"] = new MyObject("a");
m["b"] = new MyObject("b");
它们各自的优点和缺点是什么?如果有另一种方法来声明和使用 string:MyObject 字典?
最佳答案
详见 this回答(在我之前投票!),在支持 Map 之前,Typescript 中通常使用具有类型键和值的对象(有时称为 HashMap ):
const m : {[index: string]: MyObject} = {};
这种方法的问题是键只能是字符串或数字类型,实际上你使用什么作为键类型并不重要,因为数字/字符串仍然可以互换接受(只有值是强制的) .
Typescript 现在原生支持 ES6 Map 类型,它没有上述键的任何缺点。至于hashmap相对于Map的优势,我看不出有什么。
但是,如果您确实想通过索引运算符与(现在是遗留的)Map 类型进行交互,您可以通过将其包装在 Proxy 中来实现。例如
const map = new Map<string, number>();
// Set the value of "a" using Map's set function
map.set("a", 123);
const handler = {
get: (target: Map<string, number>, name: string) => { return target.get(name)},
set: (target: Map<string, number>, name: string, value: number) => {target.set(name, value)}
};
const mapWrapper = new Proxy(map, handler);
// Now add "b" to the Map with the value of 321 via the Proxy wrapper
mapWrapper["b"] = 321;
你可以看到这个例子正在运行 here .
关于performance - Map<string, MyObject> 和 {[index : string]: MyObject} in TypeScript? 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55801034/
我对 PHP 中的对象克隆有疑问。我知道克隆创建了一个“深拷贝”,因为创建了一个新对象,其变量初始化为克隆它的对象中相应变量的值。然而,as discussed here ,这意味着任何引用变量都将引
我有一个有点难以解释的合法 JS 问题。请原谅我莫名其妙的说话方式,努力把握大意。 :P 在 JS 中对象可以有属性。您可以将功能分配给属性。您可以重新定义对象的属性而不改变其其他属性。 var my
我有这种情况,我在 C# 中使用的 web 服务方法返回一个业务对象,当使用以下代码调用 web 服务方法时,我在引用中得到异常“无法将类型为 ContactInfo 的对象转换为类型 Contact
我试图进入在 ScalaDays 2013 上展示的新 Scala Pickling 库:Scala Pickling 我真正缺少的是一些如何使用库的简单示例。 我明白我可以像这样腌制一些物体并再次解
这个问题已经有答案了: Which is more effective: if (null == variable) or if (variable == null)? [duplicate] (9
这行代码做了什么? MyObject *objectOne = (MyObject *)recognizer.view; 我对 (MyObject *) 感到困惑。 它是否有效地将 recognize
我遇到了一个具有不可变属性的类: MyObject[] allObjs 属性初始化如下: List objs = createAllMyObjects(); allObjs = objs.toArra
这是我的代码: Set> mySet = getSetOf(MyObject.class); public Set getSetOf(Class setClass) { Set set =
我在面试时被问到这个问题。不能使用 Google Guava 或 MultiMap。我有课 public class Alpha { String company; int loc
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Sorting a collection of objects Sorting an ArrayList of Co
我想根据对象属性对 ArrayList 中的一些对象进行排序。该对象具有: public class Minterm { String minTerm; char flagTick;
这个比那个好吗? 这甚至是一个有效的问题吗? 我最近被告知 MyObject.DoSomething()已经过时了,服务方式是首选。那正确吗? 例子: public class Policy : IC
这个问题在这里已经有了答案: Why can't assign I to ? (2 个答案) What are the risks of explicitly casting from a lis
MyObject obj = new MyObject(); 我知道“new”关键字将调用构造函数并初始化托管堆中的对象。 我的问题是 CLR 如何执行以下操作。 CLR 如何执行上述行? CLR 如
如何转换 Flux直接到Mono> ? 我正在寻找相当于 Single> single = observable.toList()来自 RxJava。 使用阻塞运算符,我可以这样做: val just
在我的 Scalding Flow 中,我有一个 TypedType[Set[MyObject]]。如何将其展平为 TypedType[MyObject]? 最佳答案 pipe.flatMap(ide
我有很多 resharper 警告将我的参数从常规数组更改为相同类型的 IEnumerable,所以它会改变这个: public Interface IGenerator { voi
存储 Collection 的最佳方式是什么? ?该集合对于当前用户来说实际上是静态的。每个用户只能看到他们的 Collection 。 MyItem项目工具 IItem : public inter
我目前正在练习/学习 Ruby 中的单例方法和单例类,我遇到了一个疑问。鉴于: myobject = Object.new 这样做有什么区别吗: def myobject.mymethod end 然
类似于this question但使用对象而不是指针。 如果我有如下代码 Foo f; vector vect; vect.push_back(f); vect.erase(vect.begin())
我是一名优秀的程序员,十分优秀!