Imagine a straightforward indexed interface:
interface FormData {
[K: string]: string;
}
This little fella works like a charm. However, there comes a time when I want to allow a property to be an array of strings.
interface AcmeFormData extends FormData {
foobar: string[];
}
Typescript throws an error stating
Property 'foobar' of type 'string[]' is not assignable to string index type 'string'.
After checking the documentation, it appears that the following should work, but it also raises concerns.
interface FormData {
[K: string]: string;
foobar: string[];
}
I want to mention that I'd prefer to steer clear of using a union type ([K: string]: string | string[];
) because most of the time, the data will only be a single string value and therefore I would rather avoid adding type hints.
Is this doable? Or am I trying to push the limits of Typescript?