The following TypeScript sample code shows an error Element implicitly has an 'any' type because type '{one: number; two: number;}' has no index signature
in the line const one = obj[prop];
in strict mode.
The compiler allows the line const two = obj[propName];
, so I cannot understand why the error is shown or how to generally speaking access a property of an object using the bracket notation.
const obj = { one: 1, two: 2 };
const props = { one: 'one', two: 'two' };
// it is not possible add or change any properties in the props object
props.zero = 'zero';
props.one = 1;
// prop has the type string
const prop = props.one;
// using the bracket notation fails with the following error message:
// Element implicitly has an 'any' type because type '{one: number; two: number;}' has no index signature.
// const prop: string
const one = obj[prop];
// this works because propName is of type 'two'
const propName = 'two';
const two = obj[propName];