I have several types of objects, like articles, divisions, profiles, etc. I defined an interface for each, basically:
interface IArticle {
title: string;
body: string;
}
interface IProfile {
name: string;
email: string;
}
interface IDivision {
name: string;
leader: IProfile;
}
Now I want to, in some cases, be able to add a formTitle property when using these on a page that displays a form. I thought I could do something like this:
// Failed
interface IForm<T> {
formTitle: string;
}
function formDisplay(resource: IForm<IProfile>) { }
But when I do that, I get an error indicating the object properties (name and email, in this case) do not exist on type IForm<IProfile>. So I guess this is not the correct use of generics. Coming from Ruby and JavaScript, I'm still new to the whole static typing thing.
To get around this, I have been writing separate interfaces for each object, like this:
// Not reusable
interface IArticleForm extends IArticle {
formTitle: string;
}
Another alternative I could think of would be to add an optional property to a base interface and then extend the regular object interfaces from there.
// Does not provide helpful type checking
interface IBase {
formTitle?: string;
}
interface IArticle extends IBase { }
But I want formTitle to be required on these form pages so that I don't forget to set it. Is there some way to apply a group of required properties to multiple objects in a reusable way?