i have several types of objects, articles, divisions, profiles, etc. defined interface each, basically:
interface iarticle { title: string; body: string; } interface iprofile { name: string; email: string; } interface idivision { name: string; leader: iprofile; }
now want to, in cases, able add formtitle
property when using these on page displays form. thought this:
// failed interface iform<t> { formtitle: string; } function formdisplay(resource: iform<iprofile>) { }
but when that, error indicating object properties (name
, email
, in case) not exist on type iform<iprofile>
. guess not correct use of generics. coming ruby , javascript, i'm still new whole static typing thing.
to around this, have been writing separate interfaces each object, this:
// not reusable interface iarticleform extends iarticle { formtitle: string; }
another alternative think of add optional property base interface , extend regular object interfaces there.
// not provide helpful type checking interface ibase { formtitle?: string; } interface iarticle extends ibase { }
but want formtitle
required on these form pages don't forget set it. there way apply group of required properties multiple objects in reusable way?
generics intended "contain" whatever type generic - need field in iform of generic type:
interface imycontent {} interface iprofile extends imycontent { name: string; email: string; } interface iform<t extends imycontent> { formtitle: string; content: t; } var x : iform<iprofile> = { formtitle: "", content: {name: "", email: ""} }