打字稿对象作为字典类型,在C# [英] TypeScript Objects as Dictionary types as in C#
本文介绍了打字稿对象作为字典类型,在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用对象作为字典一些JavaScript code;例如人的对象将举行一些个人资料键入了电子邮件地址。
I have some JavaScript code that uses objects as dictionaries; for example a 'person' object will hold a some personal details keyed off the email address.
var people = {<email> : <some personal data'>};
adding > "people[<email>] = <data>;"
getting > "var data = people[<email>];"
deleting > "delete people[<email>];"
是否有可能来形容这打字稿?或我使用数组?
Is it possible to describe this in Typescript? or do I have to use an Array?
推荐答案
当然:
var map: { [email: string]: Customer; } = { };
map['foo@gmail.com'] = new Customer(); // OK
map[14] = new Customer(); // Not OK, 14 is not a string
map['bar@hotmail.com'] = 'x'; // Not OK, x is not a customer
您也可以让一个接口,如果你不想每次键入整个类型标注出来:
You can also make an interface if you don't want to type that whole type annotation out every time:
interface StringToCustomerMap {
[email: string]: Customer;
}
var map: StringToCustomerMap = { };
// Equivalent to first line of above
这篇关于打字稿对象作为字典类型,在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文