0
votes

I have made a table that will accept an array of objects and display them. The table will not know ahead of time what this object is and different objects could be set throughout the life cycle of the table. I am using type any[] for this object array that is coming in.

Eslint typescript-eslint plugin recommends the rule no-explicit-any which warns against using explicit any.

Is there any way I can properly type this out to avoid the use of any or is this an actual use case for using any?

1
as long as you are not trying to access the object properties you can use {}[]. If you want to access the properties then I'd suggest making an interface defining each property required and their types. - Morphyish
So going off of this I also take an HTML table and create the object array from each row to create this "advanced table". In this use case I actually need to create object properties on the fly and the only thing I know about this is that each property will in a format of number : string. Is any a good use for this? - Stevenfowler16
{[key: number]: string}[] should do it. That defines an array of object with numeric keys and string values. - Morphyish
So really if I wanted to cover an object with a variable amount of properties that could return anything when accessed(boolean, undefined, number, etc) I should use {[key: number | string]: unknown} instead of any or {}. Edit: never mind key can only be a string or number not both - Stevenfowler16
probably yeah, it's not perfect but at least you can be sure you won't pass an array of strings or numbers, so there's that. But now that I think about it, maybe using a generic type would be better if it's doable in your code. That way every time you use the table you can specify the actual type of the object used in the table. Maybe force the generic type to implement the interface you suggested here. - Morphyish

1 Answers

0
votes

So I ended up with the type {[key: string]: unknown }. Regardless of the property's type it will always be coerced back to a string or Symbol. So object[3] and object['3'] will reference the same property. Link