I'm pretty new to TypeScript and I would like to know if there exists a good way to rewrite code to avoid TSLint error "object access via string literals is disallowed" in the following code
interface ECType
{
name: string;
type: string;
elementType?: string;
}
export var fields: { [structName: string]: Array<ECType>; } = { };
class ECStruct1 {
foo: string;
bar: number;
baz: boolean;
qux: number;
quux: number;
corge: ECStruct2[];
grault: ECStruct2;
constructor() {
...
}
}
fields['ECStruct1'] = [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'int' },
{ name: 'baz', type: 'bool' },
{ name: 'qux', type: 'long' },
{ name: 'quux', type: 'ulong' },
{ name: 'corge', type: 'array', elementType: 'ECStruct2' },
{ name: 'grault', type: 'ECStruct2' }
];
Update: At the end the content above will be part of a self-generated file with more than 300 ECStruct
s, so I would like to have the class definition (e.g. ECStruct1
) followed by its meta-description (e.g. fields['ECStruct1']
).
fields['ECStruct1']
withfields.ECStruct1
. Using dot notation to access object props is usually preferred over string literal access. – Baldfields.ECStruct1=
is not allowed by the TS compiler: Error TS2339 Property 'ECStruct1' does not exist on type '{ [structName: string]: ECType[]; }'. – Padishah