Is there a way in TypeScript to define a type or an interface for an array to accept random string properties? For example, in JavaScript we can do the following:
const list = [ 1, 2, 3, 4, 5 ];
list.foo = 'bar';
console.log(a); // [ 1, 2, 3, 4, 5, foo: 'bar' ]
console.log(a[1]); // 2
console.log(a.foo); // 'bar'
I would like to define an interface or a type in TypeScript that allows such an array. I have tried the following:
interface IList extends Array<number> {
[key: string]: string;
}
But it generates a series of errors that existing array methods are not assignable to type string.
I can have specific keys set in that interface (e.g., foo: string;, but I was wondering if there is a way to allow random string properties?
list.length === 5despite the additionalfoo, and iterators (likemap) won't encounter it.