Typescript access dynamic property with [' '] syntax

javascript, typescript

Solution

This is just a convention in TypeScript, available for convenience. If you want to access some arbitrary property that is not defined in the type signature of the object, you can use the `["foo"]` notation, and the type checker will not try to enforce that the instance you're accessing has such a property in its type signature.

Problem

``` export class Foo{ someproperty: string; } ``` I am trying to understand why, when trying to access dynamic object property I can do the following as I saw on one of the answers here: ``` let fooObj: foo = someObj['someproperty']; ``` but by doing this, I get an error. ``` let fooObj: foo = someObj.someproperty; ``` I am trying to understand, why the first method works for accessing/assigning to dynamic objects. Error: `"someproperty does not exist on type"` Question asked before here, answer by Angelo R is the one I am interested in. question

Original source

Related problems