Closed
Description
consider the following class.
class GenericPractice<T>{
private entity: T;
constructor(entity: T) {
this.entity = entity;
}
public add(item: T): string {
return item.toString();
}
}
class CouponInfo {
public toString(): string {
return "couponInfo";
}
}
class Snake{
}
var genericPractice = new GenericPractice<CouponInfo>(new CouponInfo());
genericPractice.add(new Snake("Sammy the Python")));
theoretically, the above last line:
genericPractice.add(new Snake("Sammy the Python")));
should have compile time error since the generic of class GenericPractice should only allow CouponInfo in method add, not Snake.
However, the compile still passes which violates the fundamental concept of generic.
is this a bug?