【发布时间】:2016-04-16 03:21:21
【问题描述】:
我想访问带有括号符号的类型对象,如下所示:
interface IFoo {
bar: string[];
}
var obj: IFoo = { bar: ["a", "b"] }
var name = "bar";
obj[name]. // type info lost after dot
据我了解,根据spec 4.10,这是一种预期的行为:
A bracket notation property access of the form ObjExpr [ IndexExpr ]
....
Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any.
任何人都可以确认这是否属实以及是否可以规避这种行为。
编辑: 我的用例是对象缩小,如
var props = {long_name: "n"};
var shortName = props.long_name;
function(minObj) {
var value = minObj[shortName]
var newMinObj = {};
newMinObj[shortName] = value.toUpperCase();
db.save(newMinObj)
}
【问题讨论】:
标签: typescript