Home Typescript: bracket notation property access
Reply: 0

Typescript: bracket notation property access

user983 Published in May 27, 2018, 3:38 am

I'd like to access typed object with bracket notation like this:

interface IFoo {
    bar: string[];

var obj: IFoo = { bar: ["a", "b"] }
var name = "bar";
obj[name]. // type info lost after dot 

According to the spec 4.10 as far as I understood it, it is an expected behavior:

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.

Can anyone confirm if that is true and if it is possible to circumvent this behavior.

Edit: My use case is with object minification as in

var props = {long_name: "n"};    
var shortName = props.long_name;

function(minObj) {
    var value = minObj[shortName]
    var newMinObj = {};
    newMinObj[shortName] = value.toUpperCase();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303979 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO