在Java脚本中,obj['field']
也可以通过obj.field
访问对象值.但会在Typescript 中产生错误.
function F() {
const obj = { field: "firstname", title: "First Name", value: 10 };
let field = "field"
console.log(obj[field]); // <-- Typescript error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ field: string; title: string; value: number; }'
}
我怎样才能投掷obj
来解决打字错误?