我想访问带括号表示法的类型对象,如下所示:
interface IFoo {
bar: string[];
}
var obj: IFoo = { bar: ["a", "b"] }
var name = "bar";
obj[name]. // type info lost after dot
Run Code Online (Sandbox Code Playgroud)
据我所知,根据规范 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.
Run Code Online (Sandbox Code Playgroud)
任何人都可以确认这是否属实,是否有可能绕过这种行为.
编辑: 我的用例是对象缩小,如
var props = {long_name: "n"};
var shortName = props.long_name;
function(minObj) …Run Code Online (Sandbox Code Playgroud) typescript ×1