Tested with TypeScript 1.8.7
I have a situation where I need to access a property on an object with bracket notation because the property name contains dots. Unfortunately, TypeScript is less strict with the bracket notation:
interface IFoo {
n: number;
s: string;
}
function testNumber(x : number) {
return x;
}
function (foo : IFoo) {
testNumber(foo.n); // pass
testNumber(foo.s); // fail
testNumber(foo.asd); // fail
testNumber(foo['n']); // pass
testNumber(foo['s']); // fail
testNumber(foo['asd']); // pass
}
foo.asd in this case fails because asd is not a valid property of IFoo. But foo['asd'] is assumed to be of type any.
I read somewhere that this behavior is intentional to offer some kind of backdoor. I am pretty sure I do not need that backdoor in my project, but I need foo['asd'] to fail. So it would be great if this behavior could be optionally turned on.
I guess the --noImplicitAny option would have some effect, but unfortunately turning it on would require major changes in the rest of my codebase, so that is not an option.
Tested with TypeScript 1.8.7
I have a situation where I need to access a property on an object with bracket notation because the property name contains dots. Unfortunately, TypeScript is less strict with the bracket notation:
foo.asdin this case fails becauseasdis not a valid property ofIFoo. Butfoo['asd']is assumed to be of typeany.I read somewhere that this behavior is intentional to offer some kind of backdoor. I am pretty sure I do not need that backdoor in my project, but I need
foo['asd']to fail. So it would be great if this behavior could be optionally turned on.I guess the
--noImplicitAnyoption would have some effect, but unfortunately turning it on would require major changes in the rest of my codebase, so that is not an option.