I have a class Form that has this signature:
interface IFormSchema {
name: string;
// Removed irrelevant fields...
}
const schema: IFormSchema[] = [{ name: 'firstName' }];
// Form Interface
interface IForm {
fields: IFields;
// Removed irrelevant fields...
}
// Here is the IFields interface used in IForm
interface IFields {
// The key here is what I want typed **************
[key: string]: IField;
}
const form: IForm = new Form(schema: IFormSchema[]);
The schema array is iterated and each object is converted to a Field with the IField interface:
interface IField {
form: IForm;
name: string;
// Removed irrelevant fields...
}
Now, when I new up the Form and then I access the Form.fields, and can access the field by its name like so form.fields.firstName, I want firstName to be typed so that if I try and access form.fields.wrongFieldName, TypeScript will throw an error.
How would I do this? Any help would be greatly appreciated.