Sometimes you need to do something like this:
const someArray: number[] = [1, 2, 3, 4, 5, 6];
someArray['customField'] = 'dataVersion is 3';
The issue here is, that most probably makes hard for your fellow developer to read and understand that code because he/she does not expect array type to contain any custom fields. But still you might have reasons to do that, for example, ‘someArray’ is created in other far place, maybe you have no access to and is passed to your component or object. In this case you can do something like this:
interface ICustomField {
customField?: string;
}
const someArray: number[] & ICustomField = [1, 2, 3, 4, 5, 6];
someArray.customField = 'dataVersion is 3';
you can use intersection of typescript to easily create custom array type.
What do you guys think about creating custom fields in arrays? You can write it in comments.
Leave a Reply