Is there a specific term for the interface that includes a string index and generic type?
interface ___ <T> {
[index: string]: T
}
After browsing through various stack overflow examples, I've come across names like StringIndexable
, StringIndex
, StrIndex
, DictionaryIndex
, , etc.Map
For my current project, I have chosen to name it ObjectMap
. However, before implementing this in another project, I am curious if there is a recommended naming convention.
In ES6, there is the Map<K,V>
class, but there are still scenarios where an indexed object is preferable even in new code.
Why do you think there isn't a standardized name for this type? The TypeScript handbook mentions that 'string index signatures are a powerful way to describe the “dictionary” pattern', but doesn't suggest using a generic return type.
Additionally, TypeScript offers the Record<Keys, Type>
utility type which can be utilized as Record<string, T>
. The handbook recommends using a union of strings for Keys
without mentioning the option of using a catch-all string
. While it technically works, it may contradict the intended use of the type. This brings us back to the question of what to name a type like
type ___<T> = Record<string, T>
. Perhaps spelling out Record<string, Foo>
could be the solution.
So, is it common practice to fully specify the index type for each individual use case?
type Foo = any;
interface FooCollection {
[index: string]: Foo;
}
function updateFooCollection(foos: FooCollection){}
// vs
function updateFooCollection(foos: ObjectMap<Foo>){}