In your class, the <A>
indicates a generic; so, inside the class, A
refers to the generic type, ignoring any specific type or interface that you might have this symbol declared previously.
A generic type represents any type, thus you can only use the generic parameter as if it could be of any and all types, and as such you cannot access any specific propertie.
That said, in your case you should use a more specific T
type using the implements
keyword to tell the compiler the generic type T is something that implements the A
interface.
interface A {
p1: string
}
/* THIS DOES NOT WORK */
class c <T implements A> {
t1: string;
constructor(t: T){
this.t1 = t.p1
}
}
unfortunately, typescript does not allow the T implements A
expression in a generic declaration; however, there is a workaround, less expressive, by abusing the extends
keyword
interface A {
p1: string
}
class c <T extends A> {
t1: string;
constructor(t: T){
this.t1 = t.p1
}
}